NutzCN Logo
问答 pojo中枚举对应的数据库中表的字段值为空,要如何处理?
发布于 259天前 作者 wx_e4jchvgorn3d3900gcus 183 次浏览 复制 上一个帖子 下一个帖子
标签:

pojo中枚举字段,对应的数据库中表的字段值为空,报错如下:

Caused by: java.lang.RuntimeException: Fail to set ''[  ] by setter com.*****.core.team.pojo.Team.'setAdminLevel()' because [java.lang.IllegalArgumentException: No enum constant com.*******.core.team.enums.AdminLevel.]: No enum constant com.******.core.team.enums.AdminLevel.
	at org.nutz.lang.Lang.wrapThrow(Lang.java:164)
	at org.nutz.lang.inject.InjectBySetter.inject(InjectBySetter.java:48)
	at org.nutz.dao.impl.entity.field.AbstractEntityField.setValue(AbstractEntityField.java:53)
	at org.nutz.dao.impl.entity.field.NutMappingField.injectValue(NutMappingField.java:87)
	at org.nutz.dao.impl.entity.NutEntity.getObject(NutEntity.java:232)
	at org.nutz.dao.impl.sql.pojo.PojoQueryEntityCallback$1.createObject(PojoQueryEntityCallback.java:24)
	at org.nutz.dao.pager.ResultSetLooping.doLoop(ResultSetLooping.java:75)
	at org.nutz.dao.impl.sql.pojo.PojoQueryEntityCallback.invoke(PojoQueryEntityCallback.java:28)
	at org.nutz.dao.impl.jdbc.NutPojo.onAfter(NutPojo.java:109)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:277)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:53)
	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._runWithoutTransaction(NutDaoRunner.java:125)
	... 57 more
Caused by: org.nutz.castor.FailToCastObjectException: Fail to cast from <java.lang.String> to <com.******.core.team.enums.AdminLevel> for {} because:
IllegalArgumentException:No enum constant com.*******.core.team.enums.AdminLevel.
	at org.nutz.castor.Castors.cast(Castors.java:264)
	at org.nutz.castor.Castors.castTo(Castors.java:318)
	at org.nutz.lang.inject.InjectBySetter.inject(InjectBySetter.java:38)
	... 72 more
Caused by: java.lang.IllegalArgumentException: No enum constant com.*******.core.team.enums.AdminLevel.
	at java.lang.Enum.valueOf(Enum.java:238)
	at org.nutz.castor.castor.String2Enum.cast(String2Enum.java:13)
	at org.nutz.castor.castor.String2Enum.cast(String2Enum.java:6)
	at org.nutz.castor.Castors.cast(Castors.java:252)
	... 74 more

7 回复

空是指空字符串?

是的.pojo 枚举字段 对应的 数据库表中的字段值是 空串.

被这个问题头痛好久了.
1.在数据库里面给字段赋值可以解决,但是有时又没有合适的默认值;
2.现在看到报错是由 org.nutz.castor.Castors产生.想在枚举类,或POJO类里面处理一下空值,看能否行得通?

那应该设置为null吧

顺便报个issue吧,空字符串的时候枚举值就当null处理好了(●°u°●)​ 」

设置为null值是OK的.

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