-->
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.  [ 7 posts ] 
Author Message
 Post subject: Batch update
PostPosted: Tue May 10, 2005 1:35 pm 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
Hi

I am using Hibernate 2.1. I need to do a simple update to multiple rows in 'Users' table. The equivalent SQL looks like:

update users set status=1 where users_id in (1, 2, 3, 5, 6, 7);

I am hesitating to fetch Users object, modify and then save to do one simpl eupdate. Can someone tell me how to do it ?

At the moment, I am trying out following code:
Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
Query query = session.createQuery("update User set row_status_id = 1 where users_id in :usersList");
query.setParameter("usersList", "1, 2, 3, 5, 6, 7");
HibernateUtil.commitTransaction();
------

Please let me know if this needs any correction or alternate way.

Thanks
Ron


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 4:11 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Hi,

Just a tought

Wouldn't it be easier for you just to take the connection and issue a SQL statement into it? If you're not using hibernate's ability to map data to objects, you might as well go directly through the JDBC connection and update your tables..

getSession().connection().createStatement() ...

I think it would be easier for you.


Good luck,
Vincent Giguere

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 4:21 pm 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
I am using HIbernate's ability of persisting objects as much as I can. But I just think it is silly to fetch all required objects first then modify and then save them as it will end up into many database trips whereas we just need one update statement in one trip.

Thanks
Ron


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 4:40 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I understand.

What about using getSession().connection().createStatement() ...

Did it work for you?

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 9:50 am 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
Instead of creating Statement from connection, I am just fetching all objects, modifying and calling update.

Thanks for your help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 20, 2005 4:07 am 
Beginner
Beginner

Joined: Sat Sep 04, 2004 7:07 am
Posts: 20
Location: Helsinki, Finland
It seems that I can't experiment batch updates using the Eclipse plugin's HQL scatchpad. If I try I get
Code:
org.hibernate.HibernateException: Not supported for DML operations
. That's a pity, is there a workaround available?

_________________
Risto


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 20, 2005 4:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
not at the moment - add a jira request for it.

_________________
Max
Don't forget to rate


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