-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: can i undo changes on existing entities?
PostPosted: Fri Jun 13, 2008 10:49 am 
Newbie

Joined: Wed May 21, 2008 6:57 am
Posts: 8
greetings!

to put this simple, let's see a example:

Party p = em.find(Party.class, id)
PartyDetails pd = new PartyDetails;

...

// a party can have multiple details, and order number is maintained to
// connect with the latest version of details

pd.setParty(p);
p.setOrderNumber(p.getOrderNumber()+1);
pd.setOrderNumber(p.getOrderNumber());

...

so to save changes on p object practically i have to do nothing more, and to save new pd object all i have to do is em.persist(pd) and wait for transaction to happen. but what if i want to discard changes? in that case i wouldn't call persist for pd object. but can i undo the changes i made on the existing entity - p without having to retrieve data from database but simply removing it from the context.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 10:54 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This isn't quite an answer to your question, but something to think about.

After doing the initial persisting of the object, you could evict the POJO. Then, changes that happen after the evict are not applied. Then, if you do want to apply the changes, you just update or save the evicted POJO, making it a persistent object again.

So, it doesn't really reverse the update, but instead, it reverses the reversal of the reversal. :)

Just another way of thinking about it, without rolling back transactions or getting into tricky situations like that.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 11:03 am 
Newbie

Joined: Wed May 21, 2008 6:57 am
Posts: 8
very nice, but how do i evict an entity? i guess this is not ejb standard. is it compatible with seam-controlled entity manager and conversational transaction demarcation?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 8:18 am 
Newbie

Joined: Wed May 21, 2008 6:57 am
Posts: 8
i found the solution.

Session session = (Session)em.getDelegate();
session.evict(myEntity);

the thing i cannot understand is why it isn't a part of EnitityManager API because it's functionality is so basic and needed.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 3:44 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
For those playing the Hibernate3 home game, here's the Java Doc:

Quote:
evict

public void evict(Object object)
throws HibernateException

Remove this instance from the session cache. Changes to the instance will not be synchronized with the database. This operation cascades to associated instances if the association is mapped with cascade="evict".

Parameters:
object - a persistent instance
Throws:
HibernateException



http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#evict(java.lang.Object)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.