-->
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: UserType and Query parameters
PostPosted: Thu Jan 29, 2004 6:57 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
As many people, I don't like having my "business" objects making reference to Hibernate stuff. This applies to my enums as well (moreover I wanted to persit them as char instead of the default int).

So, as suggested by Gavin, I wrote a set of UserType for these objects. Now the Hibernate specific code is cleanly separated and kept in separated packages. Cool.

Unfortunately, I had some difficulties to use these objects (mapping using custom types) with the Query API.

Lets consider the following example:
- Business object/type: StatusEnum
- Corresponding user type: StatusEnumType
The Invoice business object has a property of type StatusEnum.
I want to query for all invoices having a given status, so I write the following query:

Code:
Query q = s.createQuery("from Invoice where status=:status");
q.setParameter("status", StatusEnum.OPENED);
return q.list();


This code doesn't work properly because Hibernate doesn't (apparently) detect it should use the custom StatusEnumType user type to translate the parameter into its DB representation.

The workaround was to write the query as follows:

Code:
Query q = s.createQuery("from Invoice where status=:status");
q.setParameter("status", StatusEnum.OPENED, Hibernate.custom(StatusEnumType.class));
return q.list();


I'm wondering if Hibernate could detect this situation itself, and use the appropriate user type when needed - just as it detects persistent entities from other kinds of objects. It is so easier to write the query in its first form, and less error prone (the developper doesn't have to be aware of the actual type used to persist the thing).

Feedback ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 11:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I *suppose* we could add a Configuration.registerDefaultType(Class, Type) method ... but is it really worth the API bloat?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 11:36 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Is there any way for Hibernate to remember this association from the mapping files ?

Looks to me that the mapping files already contain all the required information:
- from the property names we have the Class (by reflection)
- from the type definition we have the associated UserType...

Isn't that enough - and now API bloat ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 7:19 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
gavin wrote:
I *suppose* we could add a Configuration.registerDefaultType(Class, Type) method ... but is it really worth the API bloat?


Besides my remark above, your comment seems to be close to http://opensource.atlassian.com/projects/hibernate/secure/ViewIssue.jspa?key=HB-643

(but I'm still +1 on having Hibernate remember the class->type translations from the associations found in the mapping)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 7:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
but I'm still +1 on having Hibernate remember the class->type translations from the associations found in the mapping


Nope, I'm very against that.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 7:30 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Ah... Was it so stupid?

I understood the problem as follows:
- if you get a UserType in the mapping, then you know the class it returns;
- if you get this class as parameter to a query, then pass it through the corresponding user type...

Is it too simple? Does it break other things?


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.