-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to query for UserType parameters in HQL?
PostPosted: Wed Apr 12, 2006 12:05 pm 
Newbie

Joined: Mon Oct 31, 2005 12:01 pm
Posts: 1
Hibernate version: 3.1.2

We are using a UserType for non-default timezone dates as described in
http://www.hibernate.org/100.html.
Everything is working fine except when querying the attribute via HQL
named parameter.

The Problem is either that the UserType can NOT be passed
as (third) parameter of method org.hibernate.Query.setParameter
(since a org.hibernate.usertype.UserType is not a org.hibernate.type.Type)
or that the user type is not considered by org.hibernate.loader.hql.QueryLoader while binding parameters.

As a workaround we've wrapped the UserType as Type where the
sole real implemented method is nullSafeSet that delegates to
nullSafeSet of the user type and pass the wrapped type, i.e.

query.setParameterList(name, vals, new WrappedUserTypeQueryParameterType (new HibernateUTC.TimestampType ())

and

class WrappedUserTypeQueryParameterType implements Type {
private final UserType wrappedType;
//...
/**
* @see Type#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int,
* org.hibernate.engine.SessionImplementor)
*/
public void nullSafeSet (PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException
{
this.wrappedType.nullSafeSet (st, value, index);
}
}

rather

query.setParameterList(name, vals, Hibernate.TIMESTAMP)


Is there a better solution, did we miss something, or is this a lack/bug of Hibernate?

Thanks for any help, suggestions or comments!


Top
 Profile  
 
 Post subject: Re: How to query for UserType parameters in HQL?
PostPosted: Tue Sep 21, 2010 12:46 pm 
Beginner
Beginner

Joined: Thu Jun 21, 2007 1:47 pm
Posts: 46
I think that is what org.hibernate.type.CustomType is for.

Cheers,

Dobes


Top
 Profile  
 
 Post subject: Re: How to query for UserType parameters in HQL?
PostPosted: Tue Dec 18, 2012 9:52 pm 
Newbie

Joined: Tue Dec 18, 2012 9:46 pm
Posts: 1
Since this is near the top of certain Google results, I thought I'd add a more current answer.

To enable a UserType to also be used in a Query, you need to implement both UserType and LiteralType.

You can avoid repetitive Column and Type declarations with code that looks like the following:

Code:
@MappedSuperclass
@TypeDef(defaultForType=Foo.class, typeClass=FooType.class)
public class FooType implements UserType, LiteralType {
    // ... your implementation
}


Now any Foo value that is part of an Entity or Query will automatically use your FooType to marshal it to and from a database representation.


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