I'm a bit confused with the way JPA handle adding/updating entities.
ATM, i have this piece of code:
AltContact c = new AltContact("test");
save(c)
System.out.println("ENTITY: " + contains(c));
c.setEnterpriseName("test2");
save(c);
System.out.println("ENTITY: " + contains(c));
The save method is a simple method on the server side of my application requesting a merge on the EntityManager:
public void save (Object e) {
em.merge(e);
em.flush();
}
Where em is an instance of EntityManager
.
contains is once again a method on server side that will ask the entity manager if a given entity exist in the current persistent context.
The code above create two rows in my table, the first one with the value "test" and the other one with the value "test2", which is not what i want.
I want to create a new row with the value "test" and then, right after the creation of the row, update it and set his value to "test2". I printed out the return of contains after both call to save, false
was returned the two times.
I guess the problem come from the fact that my entity is not part of the persistent context after the first call to save so when i call save again, the entity manager consider it's a new entity and create a new row.
How can achieve this updating process?