-->
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.  [ 6 posts ] 
Author Message
 Post subject: Asking Hibernate for a copy
PostPosted: Thu Mar 08, 2007 1:31 pm 
Newbie

Joined: Fri Sep 29, 2006 6:08 am
Posts: 5
Using Hibernate 3

Hi,

Before I start, I have an error in my logic whilst using Hibernate rather than a Hibernate error in the stack trace itself. I need a Hibernate education i guess.

In English I am trying to

0 - Using AJAX I call a server method that completes and returns my model
1 - Load a model object A from the DB using Hibernate
2 - Update some variables in A
3 - return updated A to my JSP
4 - do not update the new version of A in the database

The problem

5 - I do not call save or persist on anything but Hibernate takes it upon itself to update the database

I guess that basically Hibernate is persisting my changes to object A because i have a reference to the object. I.e. when i am updating, i actually want a new copy of the object A rather than the one Hibernate has given me a reference to.

Solutions

1 - I am aware that i could write a deep clone of the object that Hibernate gives me but that is going to be very long winded in my situation. To the point that i gave up twice.

2 - ?

Is there any way i can ask Hibernate to give me a copy of an object rather that a reference?

Any help very welcome and please feel free to correct me above

Thanks

Matt


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 08, 2007 6:19 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
If you commit transaction / close session before you update the object, then you would get exactly what you are looking for.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 6:23 am 
Newbie

Joined: Fri Sep 29, 2006 6:08 am
Posts: 5
Thanks for the response.

I am using Hibernate via my application context in my DAO.

At present I call

List<Page> list = (List<Page>) getHibernateTemplate().find(sql,
c.getId());

there is no method on getHibernateTemplate() to commit transaction.

How do i commit a transaction in this context?

Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 10:14 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
if that is Spring's HibernateUtil class, it creates, and manages transactions for you. And hibernate is acting exactly as expected, it persists chagnes it detects to the database.


is serializing and using a byte stream for cloning out of the question? Easiest way IMO

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 11:11 am 
Newbie

Joined: Fri Sep 29, 2006 6:08 am
Posts: 5
Hi,

Thanks for getting back to me

I don't have permission to serialize the classes involved so couldnt use that method to create a deep clone.

I am pretty well stumped on this


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 10, 2007 6:40 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
If you don't want changes to your loaded object to be reflected in the database, you'll have to explicitly detach the loaded object form hibernate's persistence context.
Just call evict(A) on your HibernateTemplate after loading and hibernate won't care about your changes any longer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.