NutzCN Logo
问答 NutzDao.insert(final T t, boolean ignoreNull, boolean ignoreZero, boolean ignoreBlankStr) 的疑惑
发布于 2655天前 作者 MingzFan 1582 次浏览 复制 上一个帖子 下一个帖子
标签:

这个疑惑源于其中一个 test case (**SimpleDaoTest.test_insert_ignore_null()**)

先贴下错误日志:

org.nutz.dao.DaoException: !Nutz SQL Error: 'INSERT INTO t_pet(alias,age,birthday) VALUES('eMJBCIQX9y',7,'2017-09-15 10:11:02') '
PreparedStatement: 
'INSERT INTO t_pet(alias,age,birthday) VALUES(?,?,?) '
CaseMessage=Field 'name' doesn't have a default value

	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:104)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:66)
	at org.nutz.dao.impl.interceptor.SayHiDaoInterceptor.filter(SayHiDaoInterceptor.java:16)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.impl.interceptor.DaoTimeInterceptor.filter(DaoTimeInterceptor.java:23)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	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._runWithoutTransaction(NutDaoRunner.java:125)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:92)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:81)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:252)
	at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:55)
	at org.nutz.dao.impl.NutDao.insert(NutDao.java:149)
	at org.nutz.dao.impl.NutDao$17.run(NutDao.java:1033)
	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:1037)
	at org.nutz.dao.test.normal.SimpleDaoTest.test_insert_ignore_null(SimpleDaoTest.java:709)
	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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.sql.SQLException: Field 'name' doesn't have a default value
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
	at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
	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)
	... 45 more


现在来静静的看下Test的代码:

    @Test
    public void test_insert_ignore_null() {
        Pet pet = Pet.create(R.UU32());
        dao.insert(pet, true, true, true);
        assertTrue(pet.getId() > 0);
        dao.insert(Pet.create(1000), true, true, true);
    }

pet对象在create的时候已经name属性已经有值了,但在执行 dao.insert() 的时候,所有主键均被过滤而不插入到数据库,具体代码看下面:

public <T> T insert(final T t, boolean ignoreNull, boolean ignoreZero, boolean ignoreBlankStr) {
	...
	for (MappingField mf : en.getMappingFields()) {
	    if (mf.isName() || mf.isPk() || mf.isId())
	        continue;
		Object tmp = mf.getValue(obj);
		if (ignoreNull && tmp == null) {
			continue;
		}
		if (ignoreZero && (tmp == null || (tmp instanceof Number && ((Number)tmp).intValue() == 0))) {
			continue;
		}
		if (ignoreBlankStr && (tmp instanceof CharSequence && Strings.isBlank((CharSequence)tmp)))
			continue;
		names.add(mf.getName());
	}
	...
}

结果很明确,**SimpleDaoTest.test_insert_ignore_null()** 妥妥的扑街了。个人认为,上述代码应该调整为如下(去除主键的过滤,毕竟方法的定义已经很明确了,**只对值为null、0或者空白字符串的字段进行可选的过滤操作**,**并不包括主键**):

public <T> T insert(final T t, boolean ignoreNull, boolean ignoreZero, boolean ignoreBlankStr) {
	...
	for (MappingField mf : en.getMappingFields()) {
		Object tmp = mf.getValue(obj);
		if (ignoreNull && tmp == null) {
			continue;
		}
		if (ignoreZero && (tmp == null || (tmp instanceof Number && ((Number)tmp).intValue() == 0))) {
			continue;
		}
		if (ignoreBlankStr && (tmp instanceof CharSequence && Strings.isBlank((CharSequence)tmp)))
			continue;
		names.add(mf.getName());
	}
	...
}
3 回复

刚发现是改错了,正在fix

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