Hello,
In our project, we have a number of custom types; According to the hibernate documentation, we would have to do something like this: @TypeDef( name="caster", typeClass = CasterStringType.class, )
public class Forest { @Type(type="caster") public String getSmallText() { ... }
However, we wanted to do away with the need to specify @Type every time we use a custom type, and have Hibernate auto-detect the corresponding Hibernate UserType. A quick glance at the Hibernate source code tells us that we can indeed dispense with the @Type as follows:
import hello.Dog;
@TypeDef(name="hello.Dog", typeClass=hello.DogUserType.class) //'hello.Dog' is the fully-qualified class name of our custom class
class XX { private Dog dog; //No @Type annotation necessary; Hibernate realizes it has to use DogUserType.
This is possible because of the following line in SimpleValueBinder: String type = BinderHelper.isDefault( explicitType ) ? returnedClassName : explicitType; org.hibernate.mapping.TypeDef typeDef = mappings.getTypeDef( type );
If 'explicitType' is not set (via the 'Type' attribute), it uses 'returnedClassName' as the key to look-up the associated Type info. Since we have previously registered the TypeDef with the same key (hello.Dog), it is able to retrieve the value correctly.
The question is, can I RELY on this undocumented behaviour? It would make our life a lot easier, but I want to make sure I dont run into any problems later...
Regards, Sharath
|