my dog learned polymorphism*
The moose likes Object Relational Mapping and the fly likes UserType in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "UserType in Hibernate" Watch "UserType in Hibernate" New topic
Author

UserType in Hibernate

Dave Segal
Greenhorn

Joined: Aug 13, 2002
Posts: 20
Does anybody have experience with UserType in Hibernate?

From what I have read, this looks like a non-trivial endeavor that requires you to write 11 methods for each UserType class that you want to use.

Is anybody mapping properties to custom classes, or is everyone just using the native types defined by Hibernate?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Dave Segal:
Does anybody have experience with UserType in Hibernate?

From what I have read, this looks like a non-trivial endeavor that requires you to write 11 methods for each UserType class that you want to use.

Is anybody mapping properties to custom classes, or is everyone just using the native types defined by Hibernate?


I do. Sometimes you have to use UserTypes because of the vagueries of your application. But I wouldn't use them unless you have a really can't use a native type (if that isn't a fairly obvious statement).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Dave Segal
Greenhorn

Joined: Aug 13, 2002
Posts: 20
Paul - After exploring a bit more, I agree with you. I am now just mapping to aoutmatically mapped types such as string and letting my setter construct the correct type of object from the string. This works great, of course, but...

If I add a second set() method for a mapped property, Hibernate throws an exception. Do you have any tips that would allow me to add additional set methods?

Thanks,
Dave
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

A second set method? Why do you need more than one set method? I have never tried it, but I would assume that since Hibernate just uses reflection to find modifier methods for each mapped property that what you are trying should be OK. That being said, the involvement of cglib seems to complicate things.
Dave Segal
Greenhorn

Joined: Aug 13, 2002
Posts: 20
Most of my Business Objects already have set methods that take other business objects as arguments. For example:

public void setProp (Prop someProp) {this.prop = someProp;}

In many cases, the property is read from a column in the database. So, I have a choice of either setting up a custom UserType (a fair amount of work), or adding a second set property and using a native type:

public void setProp(String someProp) {this.prop = new Prop(someProp); }

I feel that the second approach is much more straightforward and less work that implementing UserType interface and mapping the types. It just requires a line of code for each property, rather than an inteface implementation and more XML for each property.

I can make the String or the UserTYpe approach work, but as soon as I have two setters in my business object, Hibernate throws an exception.

Like you, I thought that Hibernate could simply select the setter with the appropriate signature, but it can't.
 
Consider Paul's rocket mass heater.
 
subject: UserType in Hibernate
 
Similar Threads
Complex Objects with Hibernate
Creating UserType that handles binary data
Hbm2JavaTask won't generate composite user types correctly
Search using Hibernate criteria querry
Hibernate error when loading JSP