-->
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: How to preform update with where clause
PostPosted: Sun Feb 22, 2004 12:50 pm 
Newbie

Joined: Wed Nov 12, 2003 3:31 am
Posts: 9
I have an action that need to update with particular where clause such as update user set money=100 where status=1. This is a normal SQL update the affected row only affected if the status is 1. If I use hibernate dynamic update and it will update by status back to 1 if someone has changed the status to other value during the concurrent transcation and this is not my wish. I read through the hibernate doc and do show any api can set the status during a update by Criterion or Expression. Please advise how to achieve my target.

thks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 1:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
? I don't get what you mean. With hibernate, you just load the objects you want to manipulate and change their properties. Please explain your problem better.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 12:21 pm 
Newbie

Joined: Wed Nov 12, 2003 3:31 am
Posts: 9
I mean when updating a record, the record must fullfil a certain condition. For example, when i update a user bank account, i will need to confirm the account balance must be greater than 0 before proceed the update i.e. I need to write the SQL in "update user_acc set credit=100 where balance >0". In hibernate, the only API i can find is session.update(obj) or session.update(obj, id). However, I cannot find any api to support condition clause upon a update process. Yes, I can first select the balance out and compare is greater than 0 or not. However, the balance account will be continous increase/descrease by order application as well So let the database to handle this synchronization is the most effective way.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 12:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If your database handles transaction isolation properly, selecting then changing then saving won't be a problem as long as it is in one transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 12:30 pm 
Newbie

Joined: Wed Nov 12, 2003 3:31 am
Posts: 9
Yes, the db isolation level has no problem to me. Let me clear my question. In sql, I will need to run a SQL like this "update user set balance =100 where userid =1 and balance >0" in my business logic. This is what I want to do in hibernate and I don't know how.

In hibernate, I only find session.update(Objec) or session.update(Object, Serializable). But there a no way to set an condition into an update action like query, using createQuery or using criteria etc.. So, this is what I mean and so do I want to know how to do the similar in delete action like "delete from user where userid=1 and status ="inactive" " something like that.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 12:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
As I said, just load your objects, and manipulate them, there is no other way. session.delete(query) is just the same as session.load(query) and delete all of them with session.delete(object), too.


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.