I'm looking to essentially do a Hibernate merge()
or saveOrUpdate()
.
I'm new to Hibernate and Panache but my understanding is that Hibernate's merge works in situations where I wish to insert new entities (those that don't have an ID) or update existing entities (those that do have an ID) in my underlying database. This sounds very convenient and good for reducing boilerplate code!
I've seen examples like the one here which calls for using the underlying entity manager (in this case Hibernate) to do the merge, however getEntityManager()
does not appear to be available in Panache any longer. Am I looking in the wrong place?
How can I do a Hibernate merge()
using Panache? (I'm following the repository pattern)
Hibernate Reactive is not part of the JPA specification and therefore doesn't have an EntityManager. You can still access the session and merge entities using Panache.getSession()
:
Panache.getSession()
.chain( session -> session.merge( ... ) )
.chain( entity -> ... )
In Panache with JPA, Panache.getEntityManager()
is still available and you should be able to do:
Panache.getEntityManager().merge(...);
or
MyEntity.getEntityManager().merge(...);
Panache doesn't have a direct method so you have to use the one in the EntityManager
or Session
.
This sounds very convenient and good for reducing boilerplate code!
Kinda, but it's not as efficient as persisting a new entity or saving changes if you know which operation you want to do in advance.