[ACCEPTED]-How to implement update () method in DAO using EntityManager (JPA)?-entitymanager

Accepted answer
Score: 57

According to the JPA specifications, EntityManager#merge() will 6 return a reference to another object than the one 5 passed in when the object was already loaded 4 in the current context. So, I'd rather return 3 the result of the merge() and write the update() method 2 like this:

@PersistenceContext
private EntityManager em;

public User update (User transientUser) {
    return em.merge(transientUser);
}

Then, use it like this (skipping 1 the initialization part):

user.setPhone("YYYYYY");
user = dao.update(user);
Score: 2

change the property and then use EntityManager 1 merge()

http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#merge%28T%29

More Related questions