aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes New Entity with an existing primary key. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "New Entity with an existing primary key." Watch "New Entity with an existing primary key." New topic
Author

New Entity with an existing primary key.

Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
What distinguishes a New Entity with an existing primary key and a detached entity with the same primary key?


SCJP,SCWCD1.3,SCWCD1.4,SCJD,SCBCD5,SCEA5
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

hallo,

Both are detached the only difference is that one was already in the database - so you can reattach it with refresh (or merge).
I think if that the only possibility to attach a new Entity is the persist method.


Regards,
M

P.S : Interesting question !


SCJP, SCJD, SCWCD, OCPJBCD
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Mihai Radulescu wrote:
Both are detached
The entity life cycle comprises the states (see jpa spec 3.2)

new, managed, detached, removed

So "new" and "detached" isn't the same. These are different states.
you can reattach it with refresh
refresh() can only be applied to managed entities. Otherwise an IllegalArgumentException is thrown.

Mellon Sun wrote:
What distinguishes a New Entity with an existing primary key and a detached entity with the same primary key?
According to jpa spec 3.2
A new entity instance has no persistent identity, and is not yet associated with a persistence context
Unfortunately the meaning of "persistent identity" isn't defined in the spec. But in Pro EJB 3 (one of the authors was also leader in the jpa specification) on page 18 it is explained to be
An entity has a persistent identity when there exists a representation of it in the data store, that is, a row in the database.
In this sense there is no entity with state "new" whose primary key already exists in the database.

If you create a new entity instance with a primary key that does already exist in the database, this entity is in state "detached".


SCJP 5 (98%) - SCBCD 5 (98%)
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Originally posted by Mellon Sun:
What distinguishes a New Entity with an existing primary key and a detached entity with the same primary key?


I did not quite understand your question. How can two different entity references have the same primary key?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
I new an Entity and then set an already exist primary key to it.
eg:

Employee employee = new Employee();
employee.setName("mellon");
entityManager.persist(employee);

Employee employee2 = new Employee();
employee2.setName("mellon2");
employee2.setId(employee.getId());

//then
entityManager.persist(employee2);
//or
entityManager.merge(employee2);
//or
entityManager.remove(employee2);

What will happen technically?(make sense for the exam)
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
I tried:

Department department = new Department();
department.setName("Mellon");
em.persist(department);

Department department2 = new Department();
department2.setName("Mellon2");
department2.setId(department.getId());
//em.persist(department2);//javax.persistence.EntityExistsException, Cannot persist detached object
//em.merge(department2);//works well, the department name set to "Mellon2" after committed
//System.out.println(em.contains(department));//true
//System.out.println(em.contains(department2));//false
//em.remove(department2);//java.lang.IllegalArgumentException: Entity must be managed to call remove
//em.refresh(department2);//java.lang.IllegalArgumentException: Can not refresh not managed object
//em.flush();//nothing

em.merge(department2);
System.out.println(em.contains(department));//true
System.out.println(em.contains(department2));//false
try{
em.remove(department2);//after merge, still java.lang.IllegalArgumentException: Entity must be managed to call remove
}catch(Exception e){e.printStackTrace();}
em.remove(department);//after merge, works well as before
System.out.println(em.contains(department));//false
System.out.println(em.contains(department2));//false

Why after merge, department2 is still detached?
[ December 24, 2008: Message edited by: Mellon Sun ]
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
In JPA spec Page 48, "If X is a preexisting managed entity, it is ignored by the persist operation. However, the persist operation is cascaded to entities referenced by X, if the relationships from X to these other entities is annotated with the cascade=PERSIST or cascade=ALL annotation element value or specified with the equivalent XML descriptor element."

Preexisting managed entity means what? The "department" in my sample just after been persisted or an entity with the same primary key as "department2"?
[ December 24, 2008: Message edited by: Mellon Sun ]
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Mellon Sun,

nice scenario. Thanks for sharing it.
Why after merge, department2 is still detached?
persist(department) takes department into the persistence context.

merge(department2) checks wether there's already an instance of Department with the same primary key as department2 in the persistence context. If such an instance exists, let's say department, then the entity manager copies the values from the instance variables of department2 into department and returns department. You may try for example

System.out.println( ( department == em.merge(department2) ))

Generally: If department2 is not contained in the persistence context, then

department2 == em.merge(department2)
em.contains( em.merge(department2) )

are never true.
[ December 24, 2008: Message edited by: Ralph Jaus ]
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi Ralph,

you just say that :


Generally: If department2 is not contained in the persistence context, then
department2 == em.merge(department2) is false.


If an entity is not existing in the database then the merge will throw an exception. The merge method must be used for an entity that exist in the database.


Regards,
M
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Mihai,

If an entity is not existing in the database then the merge will throw an exception. The merge method must be used for an entity that exist in the database.
please have a look into the specs before posting such wrong statements. JPA spec 3.2.4.1 clearly says:
The semantics of the merge operation applied to an entity X are as follows: If X is a new entity instance, a new managed entity instance X1 is created and the state of X is copied into the new managed entity instance X1.

For short, you can say merge works as insert-update, depending on the state of the entity.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: New Entity with an existing primary key.