aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Persistence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Persistence" Watch "Persistence" New topic
Author

Persistence

Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
According to the Spec

The semantics of the persist operation, applied to an entity X are as follows:

If X is a REMOVED entity, it becomes MANAGED.

If the entity is removed would'nt it throw enity not found exception?


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
A MANAGED entity instance becomes REMOVED by invoking the remove method on it or by cascading the remove operation.

The semantics of the remove operation, applied to an entity X are as follows:

If X is a NEW entity, it is IGNORED by the remove operation. However, the remove operation is cascaded to entities referenced by X, if the relationship from X to these other entities is annotated with the cascade=REMOVE or cascade=ALL annotation element value.

What does this mean. If X is a new entity? When X is a new entity, there is no persistence identity assocated with it. So calling remove, would'nt it raise an exception.


If X is a MANAGED entity, the remove operation causes it to become REMOVED. The remove operation is cascaded to entities referenced by X, if the relationships from X to these other entities is annotated with the cascade=REMOVE or cascade=ALL annotation element value.

If X is a DETACHED entity, an IllegalArgumentException will be thrown by the remove operation (or the transaction commit will fail).

If X is a REMOVED entity, it is IGNORED by the remove operation.
If X is already removed, then again it should throw an exception

A removed entity X will be removed from the database at or before transaction commit or as a result of the flush operation.

Am I reading all these correctly?
Jim Jacob
Greenhorn

Joined: May 27, 2008
Posts: 8
Yes, this is how it is.
Calling Remove on a new entity will ignore it. Same is the case if you call remove on an entity which is already removed.
But, Calling persist on a removed entity throws EntityNotFoundException when using Hibernate as the Persistence provider. But accoring to the specification, it should become managed.



BidsEntityBean bid = new BidsEntityBean();
bid.setBidderId(20l);
bid.setItemId(30l);
bid.setBidAmount(250.00);
bid.setBidDate(null);

//Removing a New Entity == No Exceptions
entityManager.remove(bid);

//Persisting the new entity == No Exceptions
entityManager.persist(bid);

BidsEntityBean oldBid = entityManager.find(BidsEntityBean.class, new Long(23));

//Removing the entity returned by Find
entityManager.remove(oldBid);

//Removing a Removed Entity == No Exceptions
entityManager.remove(oldBid);

//Persisting a Removed Entity == Exception
//javax.persistence.EntityNotFoundException: deleted entity passed to persist:
oldBid.setItemId(40l);
entityManager.persist(oldBid);
[ June 17, 2008: Message edited by: Jim Jacob ]

[SCJP,SCWCD,SCBCD]
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
Thanks,

But as per your last statement, I don't that's going to throw an error. Because as per specs

The semantics of the persist operation, applied to an entity X are as follows:

If X is a REMOVED entity, it becomes MANAGED.

So in this case, first the entity would be removed & again it would be persisted.
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
public void persist(Object entity);

For a given entity A, the persist method behaves as follows:


If A is a NEW entity, it becomes MANAGED.
If A is an existing MANAGED entity, it is IGNORED. However, the persist operation cascades as defined below.
If A is a REMOVED entity, it becomes MANAGED.
If A is a DETACHED entity, an IllegalArgumentException is thrown.
The persist operation recurses on all relation fields of A whose cascades include CascadeType.PERSIST.

What do you understand by Removed Entity? Does it mean

em.remove(someObject);
so is someObject a removed Entity?
What do you understand by that removed entity becomes managed?

This chapter is so confusing...
Vinay Nath
Ranch Hand

Joined: Jul 06, 2008
Posts: 85
Hi Anu,

tht could be related to persistence provider caching all these statements and doing thm at some other time (eg, select or flush being called).

In tht scenario an entity tht has been removed using em.remove is being managed by tht entity manager. if some other operation is performed on it thn it may result in an exception coz it has been marked for removal.

It is necessary for entity manager to manage this entity coz otherwise tracking other accesses to this entity is not possible.


SCDJWS 5.0, SCBCD 5.0, SCWCD 5.0, SCJP 5.0
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Persistence
 
Similar Threads
Call EntityManager remove operation for removed entity.
Removed entity becomes managed?
Question about entity operations in the JPA Specification
Doubts in Enthuware mock Questions
detached entity vs. removed entity