-->
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.  [ 4 posts ] 
Author Message
 Post subject: Optimistic Locking with Custom Query
PostPosted: Thu Mar 30, 2006 12:14 am 
Newbie

Joined: Wed Mar 29, 2006 11:41 pm
Posts: 2
Hibernate 3.1.3
Annotations 3.1 Beta 9

I am trying to update an object with a custom where clause so that proper optimistic locking can be done. Is that possible?

I could not find anything in the session API to allow me to update an object and at the same time specifying a custom where clause.

Also, on a related object, how do I update an object and only update certain columns?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 12:35 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You can't use save or update or any of the other Session-provided save methods with a custom where clause. But my answer to your "On a related object" question will help...

The new HQL parser has support for an "update" keyword, so you can write a special query to update a few columns. If you want certain columns to be read-only always, you can add update="false" in their mapping.
Code:
update <ClassName> set <Prop1> = :Value1,
<Prop2> = :Value2
where <custom clause>
If HQL doesn't support whatever your optimistic locking keywords are, you can use raw SQL to do it, via an SQL query. You probably don't want to use the sql-upadte element in your mapping file, as that's always used to save, whereas a custom update query can be used when you need optimistic locking, and ignore when you don't need it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 2:50 am 
Newbie

Joined: Wed Mar 29, 2006 11:41 pm
Posts: 2
tenwit wrote:
You can't use save or update or any of the other Session-provided save methods with a custom where clause. But my answer to your "On a related object" question will help...

The new HQL parser has support for an "update" keyword, so you can write a special query to update a few columns. If you want certain columns to be read-only always, you can add update="false" in their mapping.
Code:
update <ClassName> set <Prop1> = :Value1,
<Prop2> = :Value2
where <custom clause>
If HQL doesn't support whatever your optimistic locking keywords are, you can use raw SQL to do it, via an SQL query. You probably don't want to use the sql-upadte element in your mapping file, as that's always used to save, whereas a custom update query can be used when you need optimistic locking, and ignore when you don't need it.


The custom query stuff looks cool, but I plan to play with Annotations, so no mapping files for me. Is there an annotated way to specify a custom query for save and updates for Annotations?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 5:10 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I know nothing about annotations, but you can certainly create unnamed queries in the normal way (Session.createQuery()). I presume that named queries can be specified in annotations, but I don't know how.


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