我有一个从数据库中通过Id查找对象的方法
ClassA obj = getDao().find(id);然后我更新我的对象obj.setName("newName");
之后,我保存我的对象getDao().save(obj);
在泛型DAO的方法save中,首先验证session是否包含返回false的对象
/**
* <p>
* If an entity already exists in the datastore with the same id, call
* _update and return false (not new). If no such entity exists in the
* datastore, call _save() and return true (new)
*
* @return <code>true</code> if _save(); <code>false</code> if _update().
*/
protected boolean _saveOrUpdateIsNew(Object entity) {
if (entity == null)
throw new IllegalArgumentException("attempt to saveOrUpdate with null entity");
Serializable id = getMetadataUtil().getId(entity);
if (getSession().contains(entity))
return false;
if (id == null || (new Long(0)).equals(id) || !_exists(entity)) {
_save(entity);
return true;
} else {
_update(entity);
return false;
}
}我需要从sesison hibernate中删除我的obj,或者如果有解决方法来避免这个问题。
因为我的方法返回false;以及任何保存的对象。
更新
我验证了这个条件return false
if (id == null || (new Long(0)).equals(id) || !_exists(entity)) {
_save(entity);
return true;
}id具有正确的值id= 522
发布于 2018-02-20 22:25:56
如果你想复制你的实体,你必须首先将它从会话中分离出来,将id设置为null,然后持久化它。
https://stackoverflow.com/questions/48887344
复制相似问题