I think that this is the deal. ..
Hibernate selects the setter signature
based on the return type of the getter. So, even if you have multiple setters, and one of them is consistent with the Hibernate type specified in the .hbm file, Hibernate will always select the one with a signature that is consistent with the return type of the (single) setter -
regardless of the type specified in the .hbm file.
So, if you have POJOs that use custom classes as properties, you have two choices:
1. implement the UserType interface in your custom classes, and specify them in the hbm files, or
2. Add a private get/set pair for each property with a
different name and use this "virtual property" in the .hbm files
Option 1 requires a fair amount of work at both the java level as well in all of your mapping files. And if you are going to set up multiple mapping files/pojo for purposes of altering the fetching behavior, this could turn into quite a bit of work. I am also a bit wary of this approach because I'm not that confident in my understanding of the methods specified by the interface... (
like Dirty Harry says - "A man's got to know his limitations.")
Option 2 looks a bit tacky, but if you make the methods private, who's to know?
So, if my existing bean property looks like this:
Code:
public void setSomeProp(MyCustomClass someProp);
public MyCustomClass getSomeProp();
I added methods that looked like this:
Code:
private void setSomePropString(String someProp){}
private String getSomePropString(){}
The bodies of the private get/set pair translate between MyCustomClass and String. In the mapping file, I map to the private "virtual property" like this:
<property name="somePropString" column="someColumn" type="string" />
This may not be the most elegant solution, but after about 1000 trial and error attempts, this seems like the most straight forward approach.
One more thing - be sure to check for null before returning a value for the virtual property.
I don't think that this is a bug - I think that this behavior is consistent with the java bean spec.
If anybody has a better approach, please post it!
Thanks,
Dave