Quantcast
Channel: JPA EntityManager: Why use persist() over merge()? - Stack Overflow
Browsing latest articles
Browse All 17 View Live

Answer by jarosik for JPA EntityManager: Why use persist() over merge()?

Merge won't update a passed entity, unless this entity is managed. Even if entity ID is set to an existing DB record, a new record will be created in a database.

View Article



Answer by yuranos for JPA EntityManager: Why use persist() over merge()?

Another observation:merge() will only care about an auto-generated id(tested on IDENTITY and SEQUENCE) when a record with such an id already exists in your table. In that case merge() will try to...

View Article

Answer by Peter Willems for JPA EntityManager: Why use persist() over merge()?

You may have come here for advice on when to use persist and when to use merge. I think that it depends the situation: how likely is it that you need to create a new record and how hard is it to...

View Article

Image may be NSFW.
Clik here to view.

Answer by Amit Gujarathi for JPA EntityManager: Why use persist() over merge()?

JPA is indisputably a great simplification in the domain of enterprise applications built on the Java platform. As a developer who had to cope up with the intricacies of the old entity beans in J2EE I...

View Article

Answer by Krystian for JPA EntityManager: Why use persist() over merge()?

persist(entity) should be used with totally new entities, to add them to DB (if entity already exists in DB there will be EntityExistsException throw). merge(entity) should be used, to put entity back...

View Article


Answer by Ray Hulha for JPA EntityManager: Why use persist() over merge()?

I found this explanation from the Hibernate docs enlightening, because they contain a use case:The usage and semantics of merge() seems to be confusing for new users. Firstly, as long as you are not...

View Article

Image may be NSFW.
Clik here to view.

Answer by Vlad Mihalcea for JPA EntityManager: Why use persist() over merge()?

If you're using the assigned generator, using merge instead of persist can cause a redundant SQL statement, therefore affecting performance.Also, calling merge for managed entities is also a mistake...

View Article

Answer by Ioannis Deligiannis for JPA EntityManager: Why use persist() over...

Going through the answers there are some details missing regarding `Cascade' and id generation. See questionAlso, it is worth mentioning that you can have separate Cascade annotations for merging and...

View Article


Answer by V G for JPA EntityManager: Why use persist() over merge()?

There are some more differences between merge and persist (I will enumerate again those already posted here):D1. merge does not make the passed entity managed, but rather returns another instance that...

View Article


Answer by Josep Panadero for JPA EntityManager: Why use persist() over merge()?

Persist and merge are for two different purposes (they aren't alternatives at all).(edited to expand differences information)persist: Insert a new register to the databaseAttach the object to the...

View Article

Answer by George Papatheodorou for JPA EntityManager: Why use persist() over...

Scenario X:Table:Spitter (One) ,Table: Spittles (Many) (Spittles is Owner of the relationship with a FK:spitter_id)This scenario results in saving : The Spitter and both Spittles as if owned by Same...

View Article

Answer by Khurshed Salimov for JPA EntityManager: Why use persist() over...

Some more details about merge which will help you to use merge over persist:Returning a managed instance other than the original entity is a critical part of the merge process. If an entity instance...

View Article

Answer by Raedwald for JPA EntityManager: Why use persist() over merge()?

The JPA specification says the following about persist().If X is a detached object, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or...

View Article


Answer by Sarah Vessels for JPA EntityManager: Why use persist() over merge()?

I noticed that when I used em.merge, I got a SELECT statement for every INSERT, even when there was no field that JPA was generating for me--the primary key field was a UUID that I set myself. I...

View Article

Answer by logixplayer for JPA EntityManager: Why use persist() over merge()?

I was getting lazyLoading exceptions on my entity because I was trying to access a lazy loaded collection that was in session.What I would do was in a separate request, retrieve the entity from session...

View Article


Answer by Mike for JPA EntityManager: Why use persist() over merge()?

Either way will add an entity to a PersistenceContext, the difference is in what you do with the entity afterwards.Persist takes an entity instance, adds it to the context and makes that instance...

View Article

JPA EntityManager: Why use persist() over merge()?

EntityManager.merge() can insert new objects and update existing ones.Why would one want to use persist() (which can only create new objects)?

View Article

Browsing latest articles
Browse All 17 View Live




Latest Images