-
1. Re: Extended Persistence Context and merge()
radl01 May 16, 2007 4:47 PM (in response to smithbstl)As you sead merge is used for detached objects and persist for atached ones.
Short example for persist (update action)
EntityManger em;
.....
Tab02000 recentRecord = (Tab02000)em.find(Tab02000.class, key);
.. do some changes to recentRecord
em.persist(recentRecord); // in this case JPA will call update action because you are changing existing object
Short example for merge
EntityManger em;
Tab02000 newRecord = new Tab02000();
... set all values
em.merge(newRecord);
The second example is BAD PRACTISE of using JPA
Jan -
2. Re: Extended Persistence Context and merge()
waynebaylor May 16, 2007 9:46 PM (in response to smithbstl)merge() will figure out whether or not the entity passed is new or detached. If it's new then merge() will persist it (eventually resulting in an INSERT), otherwise it will update the DB with the current state of the entity (eventually resulting in an UPDATE).
(warning: merging a removed entity will throw an exception)
check these out for more info:
http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/objectstate.html
section 3.6 and 3.7
EJB3 Persistence Specification section 3.2.4.1