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

后端 未结 15 1417
说谎
说谎 2020-11-22 03:09

EntityManager.merge() can insert new objects and update existing ones.

Why would one want to use persist() (which can only create new objec

相关标签:
15条回答
  • 2020-11-22 03:27

    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 another PersistenceException may be thrown at flush or commit time.

    So using persist() would be suitable when the object ought not to be a detached object. You might prefer to have the code throw the PersistenceException so it fails fast.

    Although the specification is unclear, persist() might set the @GeneratedValue @Id for an object. merge() however must have an object with the @Id already generated.

    0 讨论(0)
  • 2020-11-22 03:27

    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 and then try to access a collection in my jsp page which was problematic.

    To alleviate this, I updated the same entity in my controller and passed it to my jsp, although I imagine when I re-saved in session that it will also be accessible though SessionScope and not throw a LazyLoadingException, a modification of example 2:

    The following has worked for me:

    // scenario 2 MY WAY
    // tran starts
    e = new MyEntity();
    e = em.merge(e); // re-assign to the same entity "e"
    
    //access e from jsp and it will work dandy!!
    
    0 讨论(0)
  • 2020-11-22 03:31

    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 update the record. If, however, an id is absent or is not matching any existing records, merge() will completely ignore it and ask a db to allocate a new one. This is sometimes a source of a lot of bugs. Do not use merge() to force an id for a new record.

    persist() on the other hand will never let you even pass an id to it. It will fail immediately. In my case, it's:

    Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist

    hibernate-jpa javadoc has a hint:

    Throws: javax.persistence.EntityExistsException - if the entity already exists. (If the entity already exists, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.)

    0 讨论(0)
  • 2020-11-22 03:34

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

            Spitter spitter=new Spitter();  
        Spittle spittle3=new Spittle();     
        spitter.setUsername("George");
        spitter.setPassword("test1234");
        spittle3.setSpittle("I love java 2");       
        spittle3.setSpitter(spitter);               
        dao.addSpittle(spittle3); // <--persist     
        Spittle spittle=new Spittle();
        spittle.setSpittle("I love java");
        spittle.setSpitter(spitter);        
        dao.saveSpittle(spittle); //<-- merge!!
    

    Scenario Y:

    This will save the Spitter, will save the 2 Spittles But they will not reference the same Spitter!

            Spitter spitter=new Spitter();  
        Spittle spittle3=new Spittle();     
        spitter.setUsername("George");
        spitter.setPassword("test1234");
        spittle3.setSpittle("I love java 2");       
        spittle3.setSpitter(spitter);               
        dao.save(spittle3); // <--merge!!       
        Spittle spittle=new Spittle();
        spittle.setSpittle("I love java");
        spittle.setSpitter(spitter);        
        dao.saveSpittle(spittle); //<-- merge!!
    
    0 讨论(0)
  • 2020-11-22 03:34

    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 to persistence context if the entity was detached and was changed.

    Probably persist is generating INSERT sql statement and merge UPDATE sql statement (but i'm not sure).

    0 讨论(0)
  • 2020-11-22 03:35

    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 trying to use object state loaded in one entity manager in another new entity manager, you should not need to use merge() at all. Some whole applications will never use this method.

    Usually merge() is used in the following scenario:

    • The application loads an object in the first entity manager
    • the object is passed up to the presentation layer
    • some modifications are made to the object
    • the object is passed back down to the business logic layer
    • the application persists these modifications by calling merge() in a second entity manager

    Here is the exact semantic of merge():

    • if there is a managed instance with the same identifier currently associated with the persistence context, copy the state of the given object onto the managed instance
    • if there is no managed instance currently associated with the persistence context, try to load it from the database, or create a new managed instance
    • the managed instance is returned
    • the given instance does not become associated with the persistence context, it remains detached and is usually discarded

    From: http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/objectstate.html

    0 讨论(0)
提交回复
热议问题