• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

New Entity with an existing primary key.

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What distinguishes a New Entity with an existing primary key and a detached entity with the same primary key?
 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Mellon Sun
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic