NutzCN Logo
问答 数据库主键为varchar,实体类怎么写
发布于 2726天前 作者 蛋蛋的忧伤 3177 次浏览 复制 上一个帖子 下一个帖子
标签:

我数据库的主键是个字符串类型的,然后实体类里用@id就报错.

org.nutz.dao.DaoException: Field(private java.lang.String com.infogather.entity.Department.department_id) annotation @Id , but not Number type!!
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
15 回复

@Name

来自炫酷的 NutzCN

数据库字段大小写对于实体类的映射的话没有区别吧? 我加了@Name虽然不报错了,但是取出来的时候是null

什么是null?

查到了这条数据,但是主键为null

查询语句,pojo类,日志,统统贴一下

代码:

@At
    @Ok("raw")
    public Object test(){
        List<Department> list = dao.query(Department.class, null);
        System.out.println(list.get(0));
        return list.get(0);
    }

日志:

[DEBUG] 14:54:43.752 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) - Found mapping for [GET] path=/department/test : DepartmentModule.test(DepartmentModule.java:41)
Department{Department_id='null', department_name='国家海洋局北海分局', Sea_area='null', department_level='北海分局'}
[DEBUG] 14:54:45.607 com.infogather.mvc.LogTimeProcessor.process(LogTimeProcessor.java:30) - [ GET]URI=/infogather/department/test 1854ms

pojo:

package com.infogather.entity;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Name;
import org.nutz.dao.entity.annotation.Table;

/**
 * 对应数据表Department
 */
@Table("department")
public class Department {
    @Name
    private String department_id;
    @Column
    private String department_name;
    @Column
    private String sea_area;
    @Column
    private String department_level;

    public static final String DEPARTMENT_ID = "department_id";
    public static final String DEPARTMENT_NAME =  "department_name";
    public static final String SEA_AREA = "sea_area";
    public static final String DEPARTMENT_LEVEL = "department_level";

    @Override
    public String toString() {
        return "Department{" +
                "Department_id='" + department_id + '\'' +
                ", department_name='" + department_name + '\'' +
                ", Sea_area='" + sea_area + '\'' +
                ", department_level='" + department_level + '\'' +
                '}';
    }

    public String getDepartment_id() {
        return department_id;
    }

    public void setDepartment_id(String department_id) {
        department_id = department_id;
    }

    public String getDepartment_name() {
        return department_name;
    }

    public void setDepartment_name(String department_name) {
        this.department_name = department_name;
    }

    public String getSea_area() {
        return sea_area;
    }

    public void setSea_area(String sea_area) {
        sea_area = sea_area;
    }

    public String getDepartment_level() {
        return department_level;
    }

    public void setDepartment_level(String department_level) {
        this.department_level = department_level;
    }
}

没有SQL出现, 走缓存了???

我的log4j文件

log4j.rootLogger=debug,Console,file

log4j.logger.org.nutz.dao = info
log4j.logger.org.nutz.ioc = info
log4j.logger.freemarker = info
log4j.logger.org.apache.commons = info
log4j.logger.me.zouooh.mvc.quartz.QuartzIocLoader = info
log4j.logger.org.nutz.mvc.impl.Loadings = info

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%-5p] %d{HH:mm:ss.SSS} %l - %m%n


log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='-'yyyy-MM-dd-HH'.log'
log4j.appender.file.File= ${catalina.home}/logs/market/out
log4j.appender.file.Append=true
log4j.appender.file.Encoding=UTF8
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} %l - %m%n

看来是和数据库字段有关系,如果数据库主键字段为字符串并且是个全大写的,那么nutz就挂了.....应该是个bug
我刚刚把数据库主键字段改成小写的就ok,其他字段大写了@Column也可以正常的

@qq_32b899df 什么数据库?

那到底我为什么不打印sql日志呢.....

对照一下最新版的daocache配置

添加回复
请先登陆
回到顶部