-->
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: Session flush question
PostPosted: Wed Mar 14, 2007 11:11 am 
Beginner
Beginner

Joined: Wed May 31, 2006 9:24 am
Posts: 22
Hi, I have a question about session management (I read http://hibernate.org/42.html but couldn't find my answer there...).

I have 2 objects, A & B, which are completely seperate - seperate tables, mappings etc.

I create a new instance of A in memory - as yet unsaved to the database. I then call a calculation method of A to process some of the associated data (still all in memory).

As part of the calculations, I need to load an instance of object B from the database to reference a piece of information. I do this though a criteria query, calling ICriteria.UniqueResult(). At this point, the session flushes and saves object A. I do not want object A saved automatically - how do I prevent nHibernate doing this?

Hibernate version: 1.0.3.0

Regards, Ben


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 14, 2007 9:22 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
well, if i remember right, sessions can _sometimes_ be flushed before a query so that might be what is happening to you (although it might appear that it is happening _all the time_). sessions are also flushed when a transaction is committed so i don't know if you are wrapping your queries in trans but that could also be a reason for the commit.

assuming you aren't wrapping your queries in trans, then you can set the flushmode.COMMIT which will tell NH to flush only at the end of a tran (watch out for stale data...) or you could set flushmode.NEVER and manage all session.flush()ing yourself.

you can set the flushmode via a call to session.FlushMode().

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 15, 2007 5:16 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Devon's right - the default behavior is to flush the session before a query to ensure that any data returned by the query is correct and up to date based on the state of your objects.

Normally it's best to just wrap everything in a transaction and let it do the flush - if you don't want the data to stay in the database later you can roll back the transaction. If the flushed data should be reverted but the other data should be committed then you just have to update the in-memory value and the update will be flushed when you commit the transaction.

Another option is to evict the entity you don't want the data persisted for until you're happy with it's state, and then re-attach it to the session.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 15, 2007 5:34 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
I don't know nhibernate just hibernate but A wouldn't be saved if hibernate does not know it.
If hibernate saves A this mean that you did a session.save(A) before. Without doing this, hibernate does not know that A exists when you perform your query and A is not saved in the database.

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 15, 2007 7:16 am 
Beginner
Beginner

Joined: Wed May 31, 2006 9:24 am
Posts: 22
Hi guys, thanks for the responses, they've been most useful :)

@scesbron - this too was confusing me, but actually object A is part of a collection of another persisted object which why NHibernate knows about it.

@merge_s.rottem - hmm, I tried evicting the object, but it still got saved. Maybe I have to evict the parent too, but that's starting to get complicated due to various associations.

@devonl - changing the flushmode seems to work nicely. In this case, I am going to change it immediately before the query is run, and then back back to flushmode.Auto immediately after the query. That way, I don't think it will affect anything else... Can you think of any unitended consequences of doing it this way?

Thanks, ben


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 15, 2007 12:24 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Quote:
@devonl - changing the flushmode seems to work nicely. In this case, I am going to change it immediately before the query is run, and then back back to flushmode.Auto immediately after the query. That way, I don't think it will affect anything else... Can you think of any unitended consequences of doing it this way?

hmmm, i actually never thought of doing it that way. i can't see why that wouldn't work, but i haven't tried it.


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.