-->
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: Simple Update by Criteria???
PostPosted: Wed Sep 21, 2011 6:20 pm 
Newbie

Joined: Wed Sep 21, 2011 6:14 pm
Posts: 4
Hey folks,

A simple question. (And yes i searched the forum, but i was not very successful)
Is it possible to make a simple update by criteria??? Everything else works fine! So the problem is maybe just the understanding!

I mean, what if i just want to do smth. like this:

UPDATE users SET locked = true, reason = '' WHERE username like 'a%' and locked = true

And i only have the criteria, the fieldnames and values!!! I don't want to generate the HQL query by myself out of the criteria object!

Any ideas?

Thanks.


Top
 Profile  
 
 Post subject: Re: Simple Update by Criteria???
PostPosted: Thu Sep 22, 2011 2:29 am 
Newbie

Joined: Wed Sep 21, 2011 6:14 pm
Posts: 4
I mean. It cannot be the deal that i have to load all concerning entities into the RAM, just to update their fields via reflection and then save them back to the data base!
Performance???

Does really nobody have the same problem like me?


Top
 Profile  
 
 Post subject: Re: Simple Update by Criteria???
PostPosted: Thu Sep 22, 2011 4:17 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll need to use HQL (or SQL): http://docs.jboss.org/hibernate/core/3. ... tch-direct


Top
 Profile  
 
 Post subject: Re: Simple Update by Criteria???
PostPosted: Thu Sep 22, 2011 4:23 am 
Newbie

Joined: Wed Sep 21, 2011 6:14 pm
Posts: 4
That is what i am talking about! I do not want to generate the HQL by myself.

Given is a Criteria Object, the field names and the values.
I can generate the Update clause, but not the WHERE clause out of the Criteria object! That is the point!
So how can i mix a Criteria object with a HQL Query???

Or any other suggestions?

If I use HQL Queries, i am not object orientated anymore and not as dynamic as with Criteria objects!

Thx


Top
 Profile  
 
 Post subject: Re: Simple Update by Criteria???
PostPosted: Thu Sep 22, 2011 4:48 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
As far as I know the only option for UPDATE statements offered by Hibernate is to use HQL or SQL. Criterias are only for SELECTing.

Unless you are updating thousand or millions of objects there is nothing wrong with loading them into memory, updating their fields, and then let Hibernate worry about sending the UPDATE to the database. That is the object-oriented approach. Trying to mix this with DML-style operations may be a source of problems. For example, the documentation says:

Quote:
This means that manipulating data directly in the database (using the SQL Data Manipulation Language (DML) the statements: INSERT, UPDATE, DELETE) will not affect in-memory state.


Meaning that entities that you already have loaded into memory in the same session are not affected by the update so they may now contain stale data. An even worse scenario is that the in-memory entity is dirty and then the next flush/commit will overwrite what was sent with the UPDATE.


Top
 Profile  
 
 Post subject: Re: Simple Update by Criteria???
PostPosted: Thu Sep 22, 2011 5:00 am 
Newbie

Joined: Wed Sep 21, 2011 6:14 pm
Posts: 4
OK thanks! That helps a lot! :)

Bye


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.