保存Mathod返回false for Genericdao



我有一种方法,可以从数据库中找到对象

ClassA obj = getDao().find(id);

然后我更新我的对象obj.setName("newName");

之后,我保存我的对象getDao().save(obj);

在方法保存中,通用dao首先验证会话是否包含返回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;和保存的任何对象。

updat

我验证了这种情况return false

if (id == null || (new Long(0)).equals(id) || !_exists(entity)) {
            _save(entity);
            return true;
        }

ID具有正确的值ID = 522

如果要复制您的实体,则必须首先将其从会话分离,将ID设置为null,然后持续下去。

最新更新