-->
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: Unexpected implication of UserTypes in an HQL query
PostPosted: Fri Nov 05, 2004 7:16 pm 
Regular
Regular

Joined: Wed Jan 07, 2004 5:16 pm
Posts: 65
Location: CA, USA
We have implemeted a UserType to map between booleans in our managed objects and 0 or 1 value in a smallint column in DB2.

The UserType works well and as expected except for the fact that the HQL has to query against the actual value in the db table column, and not the value in the managed object. This is counterintuitive for our developers and is not what we expected from using the UserType.

For example, say we have a managed object like this:
Code:
public class Example
{
    ....
    private boolean flag;
    ....
}

which is mapped like this
Code:
    ....

    <property name="flag" type="somepackage.BooleanIntegerUserType" column="FLAG_IND" access="field"/>

    ....


To query this object and refer to this boolean property in the where clause I would expect to be able to do:
Code:
select example from Example as example where example.flag = true


but this does not work. I have to do this instead:
Code:
select example from Example as example where example.flag = 1


Are we misunderstanding the use of the UserType in this case? They are only used to map between objects and columns but are not used in anyway in HQL? It doesn't seem right that in HQL I am querying against the values of properties in my objects, but in this case this is not true - I have to query against values that are in the db?

Is there something we are missing in our UserType implementation?

If this is not possible, it would be a nice improvement in future versions of Hibernate :-)

Thanks,
Kevin Hooke

Hibernate version:
2.1.6
Mapping documents:
n/a
Code between sessionFactory.openSession() and session.close():
n/a
Full stack trace of any exception that occurs:
n/a
Name and version of the database you are using:
IBM DB2 UDB 8.1.6
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 7:27 pm 
Regular
Regular

Joined: Wed Jan 07, 2004 5:16 pm
Posts: 65
Location: CA, USA
I think I'm about to answer my own question. Is this an example of where query language substitution should be used? For example:
Code:
hibernate.query.substitutions true=1, false=0


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 5:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
UserTypes are not used in HQL right now, and they probably will never be for cases like yours.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 2:44 pm 
Regular
Regular

Joined: Mon Sep 20, 2004 8:42 am
Posts: 58
Location: Boston, US
I think this is a valid use case.

You can try the following

Code:

List result = session.createQuery("select example from Example as example where example.flag=?")
          .setParameter(0, BooleanInteger.TRUE,
                         Hibernate.custom(BooleanIntegerUserType.class))
          .list();



Sanjiv


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.