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.
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 ]
Joined: May 15, 2005
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.
Joined: May 15, 2005
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?
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.