我在使用postgresql时,定义了一个json类型的字段,插入时没有问题,但是查询时报错:
15:06:18.990 [main] INFO org.nutz.lang.inject.InjectBySetter - Fail to value by setter
org.nutz.castor.FailToCastObjectException: Fail to cast from <org.postgresql.util.PGobject> to <com.yr.cms.nutz.SystemTeam> for {{"id": "ssssss", "name": "环保社团", "domain": null, "version": null, "createTime": 0, "createUser": null, "lastUpdateTime": 0, "lastUpdateUser": null}}
at org.nutz.castor.Castors.cast(Castors.java:263)
at org.nutz.castor.Castors.castTo(Castors.java:317)
at org.nutz.lang.inject.InjectBySetter.inject(InjectBySetter.java:46)
at org.nutz.dao.impl.entity.field.AbstractEntityField.setValue(AbstractEntityField.java:53)
at org.nutz.dao.impl.entity.field.NutMappingField.injectValue(NutMappingField.java:89)
at org.nutz.dao.impl.entity.NutEntity.getObject(NutEntity.java:214)
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:114)
at org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:280)
at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:59)
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:163)
at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithTransaction(NutDaoRunner.java:109)
at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:93)
at org.nutz.dao.impl.sql.run.NutDaoRunner$1.run(NutDaoRunner.java:79)
at org.nutz.trans.Trans.exec(Trans.java:174)
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:77)
at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:242)
at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:254)
at org.nutz.dao.impl.NutDao.query(NutDao.java:490)
at org.nutz.dao.impl.NutDao.query(NutDao.java:495)
实体对象如下:
@Table("t_daoup_user")
public class SystemUser extends EntityObject {
@Column("nm")
private String name;
@Column("team")
@ColDefine(customType = "jsonb", type = ColType.PSQL_JSON)
private SystemTeam team;
...
}
public class SystemTeam{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我定位到代码发现目前nutz dao只支持map或collection,请问如何解决此问题?
public void inject(Object obj, Object value) {
Object v = null;
try {
//获取泛型基类中的字段真实类型, https://github.com/nutzam/nutz/issues/1288
Type realType = ReflectTool.getInheritGenericType(obj.getClass(), type);
Class<?> realValueType = Lang.getTypeClass(realType);
if (isMapCollection && value != null && value instanceof String) {
v = Json.fromJson(realType, value.toString());
} else {
v = Castors.me().castTo(value, realValueType);
}
if (NutConf.USE_FASTCLASS) {
if (fm == null)
fm = FastClassFactory.get(setter);
fm.invoke(obj, v);
} else {
setter.invoke(obj, v);
}
}
10 回复
肯定是进去了,只是下面这段代码有问题:只支持map或collection
public void inject(Object obj, Object value) {
Object v = null;
try {
//获取泛型基类中的字段真实类型, https://github.com/nutzam/nutz/issues/1288
Type realType = ReflectTool.getInheritGenericType(obj.getClass(), type);
Class<?> realValueType = Lang.getTypeClass(realType);
if (isMapCollection && value != null && value instanceof String) {
v = Json.fromJson(realType, value.toString());
} else {
v = Castors.me().castTo(value, realValueType);
}
if (NutConf.USE_FASTCLASS) {
if (fm == null)
fm = FastClassFactory.get(setter);
fm.invoke(obj, v);
} else {
setter.invoke(obj, v);
}
}
添加回复
请先登陆