首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为GenericDao保存方法返回false

为GenericDao保存方法返回false
EN

Stack Overflow用户
提问于 2018-02-20 22:19:51
回答 1查看 54关注 0票数 0

我有一个从数据库中通过Id查找对象的方法

代码语言:javascript
复制
ClassA obj = getDao().find(id);

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

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

在泛型DAO的方法save中,首先验证session是否包含返回false的对象

代码语言:javascript
复制
/**
     * <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

代码语言:javascript
复制
if (id == null || (new Long(0)).equals(id) || !_exists(entity)) {
            _save(entity);
            return true;
        }

id具有正确的值id= 522

EN

回答 1

Stack Overflow用户

发布于 2018-02-20 22:25:56

如果你想复制你的实体,你必须首先将它从会话中分离出来,将id设置为null,然后持久化它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48887344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档