This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UserType in Hibernate

 
Dave Segal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Dave Segal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic