-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to prevent an object from updating without evicting it?
PostPosted: Thu Aug 04, 2005 7:39 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
Hibernate 3.0.3

I have a web app upon which I'm using the Long Session pattern.

I load an object foo which has many associated, lazy connections.

Subsequently, the web framework I'm working with sets a number of fields in foo. Since foo is still associated with a session, Hibernate will write through these updates the next time the session flushes.

The problem is, this isn't what I want to happen; I need to do my own validations on the object before I determine if a save should really happen. I want hibernate to actually update the object if, and only if, I call Session.update(foo). Otherwise, I want to be able to manipulate foo to my hearts content without persisting the changes.

To an extent, I can accomplish this if I evict foo as soon as I load it e.g.

Session load(foo, id);
Session.evict(foo)

The problem with this approach though is that later on in the user's life he may, or may not, navigate to a screen which displays a lazy connection. If foo is evicted at that point, I get the infamous lazy initialization exception.

Can anyone suggest an approach whereby I can manipulate a persistent object without either A) having hibernate write through my changes automatically, or B) having to evict my object from the session?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 9:21 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
session.setFlushMode(FlushMode.NEVER)

might work for you. See api docs for details.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 9:32 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
I looked at that, but I'm not sure I understand precisely what effect it'll have based on the doc.

From what I've seen, if I set FlushMode.NEVER then the session won't flush until and unless I call session.flush().

At such time as I call flush though, *every* pending delta in that session will get pushed through.

Is this the proper mental model? If so, then I'm not sure how that's going to work out for me as it'll require I have an all or nothing flush.

Basically what happens if I have two persistent objects, foo and bar.

Both are dirty.

I want to save foo and only foo. Bar, despite being dirty, shouldn't be saved.

Would FlushMode.NEVER give me that kind of granular control? Or would both foo and bar get flushed when I call session.flush()?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 05, 2005 12:03 am 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
Hmmm, just had a thought...

Could I do this with two sessions?

Something like ...

Session query = SessionFactory.createSession();
query.setFlushMode(FlushMode.NEVER);
Session io = SessionFactory.createSession();

query.load(foo);
query.load(bar);

foo.setSomeProperty(aValue);
bar.setSomeProperty(anotherValue);

io.merge(foo);

Has anyone tried an approach like the above? Can you think of any obvious pitfalls (apart from having to keep two sessions open)?


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