NutzCN Logo
问答 关于dao.create()时,使用@TableIndexes报错
发布于 47天前 作者 Saviour 132 次浏览 复制 上一个帖子 下一个帖子
标签:

nutz版本:nutz-1.r.60
问题描述:
在Setup中存在如下代码

Daos.createTablesInPackage(dao, "com.kerbores", false);// 创建表 line99
Daos.createTablesInPackage(dao, "com.melo", false);// 创建表 line100
Daos.migration(dao, "com.kerbores", true, true, true);// 更改表 line101
Daos.migration(dao, "com.melo", true, true, true);// 更改表 line102

实体类代码如下:

@Table("h_notice_user_sync_record")
@Comment("站内通知同步记录")
@TableMeta("{'mysql-charset':'utf8mb4'}")
@TableIndexes(@Index(fields = {"user_id", "user_type"}, name = "h_notice_user_sync_record.unique_idx"))
public class NoticeUserSyncRecord extends Entity {

    @Column("user_id")
    @Comment("用户Id")
    @ColDefine(notNull = true)
    private int userId;

    @Comment("用户类型")
    @Column("user_type")
    @ColDefine(notNull = true)
    private User.Type userType;

    @Comment("通知Id")
    @Column("notice_id")
    private int noticeId;

    @Column("sync_time")
    @Comment("最后同步时间")
    private Date syncTime;
}

Entity中有个自增主键id
当实体类没有@TableIndexes这一行注解且数据库无表h_notice_user_sync_record时,可以成功创建表
当增加@TableIndexes注解后,
1.当数据库无表h_notice_user_sync_record时,出现以下报错

05-25 21:20:55.492  DEBUG - [Statement.RMI TCP Connection(3)-127.0.0.1] {conn-10010, stmt-20044} created
05-25 21:20:55.571  ERROR - [Statement.RMI TCP Connection(3)-127.0.0.1] {conn-10010, stmt-20044} execute error. SELECT * FROM h_notice_user_sync_record LIMIT 1
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'qieguang_test1.h_notice_user_sync_record' doesn't exist
	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)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
	at com.mysql.jdbc.Util.getInstance(Util.java:408)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2482)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2440)
	at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1381)
	at com.alibaba.druid.filter.FilterChainImpl.statement_executeQuery(FilterChainImpl.java:2363)
	at com.alibaba.druid.filter.FilterAdapter.statement_executeQuery(FilterAdapter.java:2481)
	at com.alibaba.druid.filter.FilterEventAdapter.statement_executeQuery(FilterEventAdapter.java:302)
	at com.alibaba.druid.filter.FilterChainImpl.statement_executeQuery(FilterChainImpl.java:2360)
	at com.alibaba.druid.filter.FilterAdapter.statement_executeQuery(FilterAdapter.java:2481)
	at com.alibaba.druid.filter.FilterEventAdapter.statement_executeQuery(FilterEventAdapter.java:302)
	at com.alibaba.druid.filter.FilterChainImpl.statement_executeQuery(FilterChainImpl.java:2360)
	at com.alibaba.druid.proxy.jdbc.StatementProxyImpl.executeQuery(StatementProxyImpl.java:211)
	at com.alibaba.druid.pool.DruidPooledStatement.executeQuery(DruidPooledStatement.java:140)
	at org.nutz.dao.impl.jdbc.AbstractJdbcExpert.setupEntityField(AbstractJdbcExpert.java:80)
	at org.nutz.dao.impl.entity.AnnotationEntityMaker._checkupEntityFieldsWithDatabase(AnnotationEntityMaker.java:552)
	at org.nutz.dao.impl.entity.AnnotationEntityMaker.make(AnnotationEntityMaker.java:278)
	at org.nutz.dao.impl.EntityHolder.getEntity(EntityHolder.java:71)
	at org.nutz.dao.impl.NutDao.getEntity(NutDao.java:655)
	at org.nutz.dao.util.Daos.createTablesInPackage(Daos.java:525)
	at com.melo.ylhy.setup.Setup.init(Setup.java:100)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:277)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4590)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5233)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
	at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1736)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
	at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
	at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482)
	at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
	at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
	at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
	at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
	at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
	at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
	at java.security.AccessController.doPrivileged(Native Method)
	at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)
	at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
	at sun.rmi.transport.Transport$1.run(Transport.java:200)
	at sun.rmi.transport.Transport$1.run(Transport.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:835)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
05-25 21:20:55.578  DEBUG - [AbstractJdbcExpert.RMI TCP Connection(3)-127.0.0.1] Table 'h_notice_user_sync_record' doesn't exist!
05-25 21:20:55.578  DEBUG - [Statement.RMI TCP Connection(3)-127.0.0.1] {conn-10010, stmt-20044} closed
05-25 21:20:55.578  DEBUG - [Connection.RMI TCP Connection(3)-127.0.0.1] {conn-10010} pool-recycle
05-25 21:20:55.578  ERROR - [NutLoading.RMI TCP Connection(3)-127.0.0.1] Error happend during start serivce!
java.lang.RuntimeException: Fail to find field 'user_id' in 'com.melo.ylhy.bean.notice.NoticeUserSyncRecord' by @Index(h_notice_user_sync_record.unique_idx:user_iduser_type)
	at org.nutz.lang.Lang.makeThrow(Lang.java:132)
	at org.nutz.dao.impl.entity.AnnotationEntityMaker._evalEntityIndexes(AnnotationEntityMaker.java:536)
	at org.nutz.dao.impl.entity.AnnotationEntityMaker.make(AnnotationEntityMaker.java:290)
	at org.nutz.dao.impl.EntityHolder.getEntity(EntityHolder.java:71)
	at org.nutz.dao.impl.NutDao.getEntity(NutDao.java:655)
	at org.nutz.dao.util.Daos.createTablesInPackage(Daos.java:525)
	at com.melo.ylhy.setup.Setup.init(Setup.java:100)

从报错内容来看是Table 'h_notice_user_sync_record' doesn't exist!导致的
2.如果表存在,不会创建索引
希望解答:
是否使用方式不正确?如果是,正确使用方式应该如何写?
如果是nutz版本问题,由于项目原因无法更新,就采用手工建索引方式解决

2 回复

索引名不要写 表名.xx

顶多写个 yyy_xxx ,不要包含点号

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