-->
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: Custom SQL on Update
PostPosted: Thu Sep 30, 2004 4:18 pm 
Newbie

Joined: Fri Feb 27, 2004 11:44 pm
Posts: 8
Hibernate version: 2.1.6

Mapping documents: Partial definition follows.
Code:
        <id
            name="id"
            type="java.lang.Long"
            column="id"
        >
            <generator class="native"/>
        </id>


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Data base server: Postgres 7.4.3

The generated SQL (show_sql=true):
Code:
update WeatherArchive set rain_mm=?, ...etc, where id=?



Is there a way to force the generated SQL for an update to include an explicit cast on the columns used in the where clause?

For instance, I need the above update SQL to look like this:
Code:
update WeatherArchive set rain_mm=?, ...etc, where id::bigint?


Notice the cast of the id column in the where clause. The reason I ask has to do with the way Postgres uses (or, in my cast, does not use) indexes. If the value assigned to id in the update statement is an int (int4, the default) a sequential scan results. If the value assigned to id is casted as a bigint (int8) an index scan results.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 01, 2004 10:08 am 
Newbie

Joined: Fri Feb 27, 2004 11:44 pm
Posts: 8
Since, no one has responded I'll assume this cannot be done without reinstrumenting hibernate.

Has anyone else come across this problem with Postgres?

Rob


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 01, 2004 3:06 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
If you're not in a hurry, maybe Hibernate 3 helps. See Gavins blog at http://blog.hibernate.org/cgi-bin/blosxom.cgi/Gavin%20King/customsql.html.

The reference manual of the alpha 3 release has also some information.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 01, 2004 4:42 pm 
Newbie

Joined: Fri Feb 27, 2004 11:44 pm
Posts: 8
Thanks for the information. I'll look at the Hibernate3 docs.

For the record, the SQL for the update with explicit cast should have been:
Code:
update WeatherArchive set rain_mm=?, ...etc, where id=?::bigint


Where the value assigned to column 'id' is explicitly casted to a bigint. Being able to turn on explicit casting of columns that have an index would be a boon for me.

Rob


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 03, 2004 9:33 pm 
Newbie

Joined: Fri Feb 27, 2004 11:44 pm
Posts: 8
OK, with further research it looks like I can accomplish what I need by writing my own custom ClassPersister that generates the update sql as I need it. The ClassPersister is associated with the entity class by the
Code:
<class
   ...
   persister="PersisterClass"
   ...
/>

persister attribute in the Class element of the Hibernate mapping file for the entity class.

Does anyone know any other way of doing this?

Rob


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 03, 2004 10:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
In HB3, it can be done in the mapping document. In HB2, you need a custom persister.


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.