• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Persistence

 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic