如何在JPA中复制Hibernate的saveOrUpdate?

Jam*_*hon 38 java persistence hibernate jpa

在JPA中,有什么方法可以复制Hibernate的saveOrUpdate行为,

saveOrUpdate

public void saveOrUpdate(Object object)
                  throws HibernateException

    Either save(Object) or update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

    This operation cascades to associated instances if the association is mapped with cascade="save-update".

    Parameters:
        object - a transient or detached instance containing new or updated state 
    Throws:
        HibernateException
    See Also:
        save(Object), update(Object)
Run Code Online (Sandbox Code Playgroud)

它实质上检查数据库中是否已存在该对象,并根据需要更新该对象或保存该对象的新实例.

JPA无交易读取很好,但我真的很想从Hibernate中获取这个方法.经验丰富的JPA开发人员如何处理这个?

Pab*_*jim 29

尝试使用该EntityManager.merge方法 - 这非常相似.

Xebia博客文章中的差异有很好的描述:" JPA实施模式:保存(独立)实体".

  • 对我来说最重要的是"当更新现有实体时,我们不会调用任何EntityManager方法; JPA提供程序将在刷新或提交时自动更新数据库." (6认同)
  • 链接已死。这是新的:http://blog.xebia.com/jpa-implementation-patterns- saving-detached-entities/ (2认同)

Jam*_*hon 5

Pablojim 链接的文章中概述的方法的问题在于它不能很好地处理自动生成的主键。

考虑创建一个新的 ORM 实体对象,您可以给它与数据库表中现有行相同的数据,但除非我弄错了,实体管理器不会将它们识别为同一行,直到它们具有相同的主键,在使用自动生成的密钥的实体中,您在进入数据库之前无法获得。

这是我目前针对这种情况的工作;

/**
 * Save an object into the database if it does not exist, else return
 * object that exists in the database.
 *
 * @param query query to find object in the database, should only return
 * one object.
 * @param entity Object to save or update.
 * @return Object in the database, whither it was prior or not.
 */
private Object saveOrUpdate(Query query, Object entity) {
    final int NO_RESULT = 0;
    final int RESULT = 1;

    //should return a list of ONE result, 
    // since the query should be finding unique objects
    List results = query.getResultList();
    switch (results.size()) {
        case NO_RESULT:
            em.persist(entity);
            return entity;
        case RESULT:
            return results.get(0);
        default:
            throw new NonUniqueResultException("Unexpected query results, " +
                    results.size());
    }
}
Run Code Online (Sandbox Code Playgroud)