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."