-->
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: Calling Critieria.List() internally calls SaveOrUpdate?
PostPosted: Fri Jun 01, 2007 5:32 pm 
Newbie

Joined: Tue May 29, 2007 4:57 pm
Posts: 7
At least that is what I am seeing.

For example:

Job job = null;
Type type = job.GetType();

//Loading up a job for the UI
job = (Job) session.Get(type, "SomeID");


//User can change TicketNum in UI
job.TicketNum = "NewTicketNum"

//Checking to see if a job already exists with this ticket num
ICriteria crit = session.CreateCriteria(type);
crit.Add(Expression.Eq("TicketNum", "NewTicketNum"));


//At this point when this is called, it saves the TicketNum I just assigned--not desirable
IList list = crit.List();

I cannot use TicketNum as the ID (database replication), but it must be unique to the user on his system. I have specified a unique constraint, but for me to figure out if they have violated the constraint by way of exception, requires me to do something awful like this:

exception.Message.Contains("UNIQUE KEY");

Because there aren't different SQL exceptions.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 01, 2007 5:58 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Essentially any persistent object (which your job is, since you loaded it) may have its state flushed to the database when you do a query to ensure the query results are in a consistent state.

I made a post in the following thread trying to clarify how transparent persistence works and there's some good info there that covers your question - maybe it'll help clear things up for you: http://forum.hibernate.org/viewtopic.php?t=974039

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 01, 2007 6:30 pm 
Newbie

Joined: Tue May 29, 2007 4:57 pm
Posts: 7
Ah yes, very helpful. Changed my FlushMode and that fixed things right up.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 02, 2007 5:41 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Keep in mind that if you change the flush mode you may get back a result set that doesn't include changes you've made between loading an object and flushing it.

For example, say you're going to load a couple of jobs, modify their description to add the keyword "foo" and then execute a query for all jobs that have the keyword "foo" in the description all in a single unit of work. If you don't allow the flush before the query is executed the results that come back from the database will not include the jobs you changed, so they won't appear in the result set.

If you're running in a transaction it's often OK to let the flush happen since if you decide you don't want to commit the unit of work you can roll back the transaction and everything will be undone.

Just worth being aware of.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 11:12 am 
Newbie

Joined: Tue May 29, 2007 4:57 pm
Posts: 7
I appreciate the warning. The flush mode I have it set to is "NHibernate.FlushMode.Commit", which seems more natural to me. I am not accustomed to having data sent back to the DB without me explicitly saying so.


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.