你如何在JPA中复制Hibernate的saveOrUpdate? [英] How can you replicate Hibernate's saveOrUpdate in JPA?

查看:3066
本文介绍了你如何在JPA中复制Hibernate的saveOrUpdate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JPA,有可以复制Hibernate的 saveOrUpdate行为

  saveOrUpdate 

public void saveOrUpdate(Object object)
抛出HibernateException

根据未保存值检查的解决方案,保存(Object)或更新(Object)给定实例(参见手册以讨论未保存值检查)。

如果关联映射为cascade =save-update,则此操作级联到关联的实例。

参数:
对象 - 包含新状态或更新状态的临时或分离实例
抛出:
HibernateException
另请参见:
save(对象),更新(对象)

基本上检查数据库中是否存在对象可以根据需要更新该对象或保存该对象的新实例。

JPA transcationless read很不错,但我真的从Hibernate中遗漏了这个方法。有经验的JPA开发人员如何处理这个问题?

解决方案

尝试使用 EntityManager.merge 方法 - 这是非常相似的。



Xebia的博文中有一个很好的描述: JPA Implementation Patterns:Saving(Detached)Entities


In JPA, is there any way you can replicate Hibernate's saveOrUpdate behavior,

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)

which essentially checks to see if the object already exists in the database and either updates that object as need be or saves a new instance of the object.

JPA transcationless reads are nice, but I am really missing this method from Hibernate. How do experienced JPA developers handle this?

解决方案

Try using the EntityManager.merge method - this is very similar.

There is an excellent description of the differences in Xebia's blogpost: "JPA Implementation Patterns: Saving (Detached) Entities."

这篇关于你如何在JPA中复制Hibernate的saveOrUpdate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆