-->
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.  [ 2 posts ] 
Author Message
 Post subject: Update field in multiple rows
PostPosted: Mon Feb 20, 2006 12:40 pm 
Newbie

Joined: Wed Feb 15, 2006 3:26 pm
Posts: 5
Hello!

I need to update one single field in multiple rows in a table which fulfill some specific criteria. Right now I'm using CreateCriteria to get all affected rows and iterate through the list setting the field to it's new value and saying SaveOrUpdate. As I open and close the session for every query (here: SaveOrUpdate) this isn't very fast. Is there a better way to do this?


Top
 Profile  
 
 Post subject: Re: Update field in multiple rows
PostPosted: Mon Feb 20, 2006 3:33 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
cartilla wrote:
Hello!

I need to update one single field in multiple rows in a table which fulfill some specific criteria. Right now I'm using CreateCriteria to get all affected rows and iterate through the list setting the field to it's new value and saying SaveOrUpdate. As I open and close the session for every query (here: SaveOrUpdate) this isn't very fast. Is there a better way to do this?


I don't know if there is a real performance boost, but usually in a situation like this I'll open a single unused session just for it's connection, then build the further sessions using it's connection, like so:

Code:
ISession super = sessions.OpenSession();
ISession load = sessions.OpenSession( super.Connection );

IList objs = null;
try {
   objs = load...
}
finally {
   load.Close();
}

ISession update = null;
foreach ( Object o in objs ) {
   // ... do stuff to object
   update = sessions.OpenSession( super.Connection );
   try {
      update.Update( o );
      update.Flush();
   }
   finally {
      update.Close();
   }
}

super.Close();


With connection pooling, I'm not sure that's really saving any time, but it's what I use. Also, I only do this when there's a large number of updates to do (not really sure about a threshold, though; my "large" is pretty vague). If there's only a few objects, I wouldn't bother building a session for each update.

Obviously, if anyone knows that this is a particularly bad or superfluous pattern, let me know!

I suppose it would be nice to have an update available through HQL, but I think that is frowned upon in certain quarters, since the changes are supposed to be done through the business object framework. Still, there are times when the "quick and dirty" solution is handy.


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