Registering a Custom UserType with Hibernate 5 Without @Type Annotations Everywhere
When you have a custom UserType
but do not want to specify @Type
at every usage, you can register the type with Hibernate.
This solution is for Hibernate 5.*
Create a class that implements org.hibernate.boot.spi.SessionFactoryBuilderFactory
, and in the getSessionFactoryBuilder()
method, you can register a new type using metadata.getTypeResolver().registerTypeOverride(userType, keys)
.
metadata.getTypeResolver().registerTypeOverride(MonetaryAmountUserType.INSTANCE, new String[]{"javax.money.MonetaryAmount"});
The SessionFactoryBuilderFactory implementation is picked up using the Java ServiceLoader mechanism. You need a file named with the FQDN of the service interface, e.g., org.hibernate.boot.spi.SessionFactoryBuilderFactory, which must be placed in the META-INF/services directory.
When using Gradle, simply create the directory META-INF/services in the resources folder of the project.
Problem
Unfortunately, this does not work when using a CompositeUserType (a user type mapping to multiple columns). This is because Hibernate validates the column count of the metadata. Your CompositeUserType will return the correct number of columns (e.g., 2), but the field in the entity where you use your type will report one column. You can specify the column names with @Columns on the field, but this defeats the purpose of not having to repeat annotations everywhere your UserType is used.