The JPA specification states that:If X is a new entity instance, a new managed entity instance X' is created and the state of X is copied into the new managed entity instance X'.
So I tried to understand the above from two examples below:-
Case 1:-
entityManager.getTransaction().begin();
//let's assume we dont have a employee with id 2 in database
Employee e=new Employee();
e.setId(2);
e.setName("john");
/*
If I understand correctly the below query should run just an INSERT query
when context is flushed
but hibernate runs two queries,SELECT followed by INSERT
*/
entityManager.merge(e);
// entityManager.getTransaction().commit();
Case 2:-
entityManager.getTransaction().begin();
//let's assume we have a employee with id 3 in database
Employee e=new Employee();
e.setId(3);
e.setName("arthur");
/*
If I understand correctly the below query should fail to run because
then we would have two records with same primary key when context is flushed
but hibernate runs two queries,SELECT followed by UPDATE
*/
entityManager.merge(e);
// entityManager.getTransaction().commit();
I'm new to hibernate so is my understanding of merge correct and if not what am I missing? Also it seems merge works for transient entities too,is it right?
You'll want to read the JPA specification for merge behavior - but the API has little to do with inserts; you might be confused with em.persist(e)? Merge takes a detached (or new entity) and merges it into the context. That requires loading it into the context if it isn't there already, which accounts for the select, and then merging in the changes from your entity into the managed instance it will return. If it doesn't exist, it'll perform an insert when required (flush/commit etc), otherwise it'll perform an update when required.