NutzCN Logo
问答 自定义生成主键 出错
发布于 2719天前 作者 qq_a930202f 1951 次浏览 复制 上一个帖子 下一个帖子
标签:
	@Inject
	private UidGenerator uidGenerator;

       @Id(auto=false)
	@Prev(els=@EL("$me.genUID()"))
	protected Long id;

	public Long genUID(){
		return uidGenerator.getUID();
	}

报错信息

Caused by: java.lang.NullPointerException
	at net.common.persistence.Entity.genUID(BuidEntity.java:40)
	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.nutz.lang.Invoking$NullArgInvoker.invoke(Invoking.java:77)
	at org.nutz.lang.Invoking.invoke(Invoking.java:192)
	... 86 more
19 回复

坑啊,看nutz-1.r.55-manual上面都没有

文档是持续更新的

@Prev(els=@EL("ig(view.tableName)")) // view.tableName 相当于 dao.getEntity(pojo.getClass()).getTableName()

view.tableName在这里的作用是什么?是固定写法,还是变量

变量来的,文档不是写了吗?

看源码看明白了。。。。
但是我这边,有出错

2017-07-12 14:07:52 DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:97) SQLException
java.sql.SQLException: Field 'id' doesn't have a default value
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)

看样子是没注册成功?
我该如何检查问题出在哪里,我看日志MySuperIdGenerator已经加载了

@Id(auto=false)

加了

	@Id(auto=false)
	@Prev(els=@EL("buid()"))
	protected Long id;
    public String fetchSelf() {
        return "buid";
    }
	    CustomMake.me().register("buid",ioc.get(MySuperIdGenerator.class)); 

贴完整报错信息

@wendal

2017-07-12 16:17:09 DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) // NOT SQL // ElFieldMacro=buid()
2017-07-12 16:22:34 DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) // NOT SQL // ElFieldMacro=$me.now()
2017-07-12 16:22:53 DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) INSERT INTO article(title,hits,up,down,sort,status,type) VALUES(?,?,?,?,?,?,?) 
    |                            1 | 2 | 3 | 4 | 5 | 6 | 7 |
    |------------------------------|---|---|---|---|---|---|
    | Wed Jul 12 16:17:09 CST 2017 | 0 | 0 | 0 | 0 | 0 | 0 |
  For example:> "INSERT INTO article(title,hits,up,down,sort,status,type) VALUES('Wed Jul 12 16:17:09 CST 2017',0,0,0,0,0,0) "
2017-07-12 16:22:53 DEBUG com.alibaba.druid.pool.PreparedStatementPool.put(PreparedStatementPool.java:123) {conn-10001, pstmt-20011} enter cache
2017-07-12 16:22:53 DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:97) SQLException
java.sql.SQLException: Field 'id' doesn't have a default value
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2535)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1911)
	at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1203)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2931)
	at com.alibaba.druid.wall.WallFilter.preparedStatement_execute(WallFilter.java:600)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2929)
	at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2929)
	at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:131)
	at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:493)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:308)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:90)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:66)
	at org.nutz.dao.impl.interceptor.DaoLogInterceptor.filter(DaoLogInterceptor.java:22)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.DaoInterceptorChain.invoke(DaoInterceptorChain.java:139)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.runCallback(NutDaoRunner.java:158)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithTransaction(NutDaoRunner.java:104)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:88)
	at org.nutz.dao.impl.sql.run.NutDaoRunner$1.run(NutDaoRunner.java:74)
	at org.nutz.trans.Trans.exec(Trans.java:174)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:72)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:248)
	at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:50)
	at org.nutz.dao.impl.NutDao.insert(NutDao.java:145)
	at org.nutz.dao.impl.NutDao$15.run(NutDao.java:996)
	at org.nutz.dao.FieldFilter.run(FieldFilter.java:286)
	at org.nutz.dao.FieldFilter.run(FieldFilter.java:297)
	at org.nutz.dao.impl.NutDao.insert(NutDao.java:1000)
	at org.nutz.service.EntityService._insert(EntityService.java:363)
	at net.common.service.BaseService.insert(BaseService.java:168)
	at net.web.UidModule.uid(UidModule.java:38)
	at net.web.UidModule$$FASTCLASS._invoke(UidModule.java:1)
	at org.nutz.lang.reflect.AbstractFastClass.invoke(AbstractFastClass.java:143)
	at org.nutz.lang.reflect.FastClassFactory.invoke(FastClassFactory.java:67)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at net.web.processor.LogTimeProcessor.process(LogTimeProcessor.java:23)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:198)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.bufferAndPostProcess(ContentBufferingFilter.java:169)
	at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.doFilter(ContentBufferingFilter.java:126)
	at org.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:120)
	at org.sitemesh.config.ConfigurableSiteMeshFilter.doFilter(ConfigurableSiteMeshFilter.java:163)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

插入的字段列表里面没id,奇怪了,@Id(auto=false) 没生效? 是不是没编译没重启?

刚又重新编译重启多次,还是同样的问题
Debug到Object value = bin.eval(context);这个value是有值的,是不是表明自增的部分正常

public void onAfter(Connection conn, ResultSet rs, Statement stmt) throws SQLException {
        Context context = entityField.getEntity()
                                     .wrapAsContext(getOperatingObject());
        context.set("field", entityField.getColumnName());
        context.set("view", entityField.getEntity());
        Object value = bin.eval(context);
        entityField.setValue(getOperatingObject(), value);
    }

接着debug就报错了,没太看懂报错处的相关代码。

跟这个方法没啥关系,现在是id字段依然认为是自增,所以没出现在insert语句中

试了版本
<nutz.version>1.r.60</nutz.version>
<nutz.version>1.r.61.r2</nutz.version>
都出现这个问题

@wendal
id改成基本类型就好了。。。。
这个怎么引起的,建议最好兼容对象类型。

额,Long,感觉是bug,要不你先报个issue吧?

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