How to customize the HibernateTypesContributor from Hypersistence Utils
Imagine having a tool that can automatically detect JPA and Hibernate performance issues. Wouldn’t that be just awesome?
Well, Hypersistence Optimizer is that tool! And it works with Spring Boot, Spring Framework, Jakarta EE, Java EE, Quarkus, or Play Framework.
So, enjoy spending your time on the things you love rather than fixing performance issues in your production system on a Saturday night!
Introduction
In this article, we are going to see how to customize the HibernateTypesContributor from Hypersistence Utils so that we can disable all or filter out certain Types.
What does HibernateTypesContributor does in Hypersistence Utils
The HibernateTypesContributor
implements the TypeContributor
Hibernate interface and registers several Types automatically. By doing this, the audited entities will be able to locate the Hypersistence Utils Types that you used in your JPA entities.
Hibernate ORM uses the Java Service Provider Interface mechanism to register custom implementations of various Hibernate APIs that allows you to customize the way Hibernate works.
To register a custom TypeContributor
, you can provide the following file on your Java classpath:
META-INF/services/org.hibernate.boot.model.TypeContributor
And the org.hibernate.boot.model.TypeContributor
file content should contain the fully-qualified class name of the Java class implementing the org.hibernate.boot.model.TypeContributor
interface.
For instance, the Hypersistence Utils library provides the following org.hibernate.boot.model.TypeContributor
file:
io.hypersistence.utils.hibernate.type.HibernateTypesContributor
By doing so, Hibernate will locate this file and initialize it at bootstrap time.
How to control the Types registered by HibernateTypesContributor
Since version 3.5, Hypersistence Utils now allows you to customize the automatic Type registration.
The customization is done via the standard Hibernate setting mechanism, which you can set using the spring.jpa.properties
prefix in your application.properties file.
By default, The Hypersistence Utils Types are automatically registered, so if you want to disable this process, then you can set the following property:
spring.jpa.properties.hypersistence.utils.enable_types_contributor=false
This setting is useful when you suspect an issue due to a Hypersistence Utils Type that replaces a standard Hibernate ORM Type and changes the default data mapping behavior that you were expecting. By disabling the automatic registration for all Hypersistence Utils Types, you can validate whether the problem is caused by the Hypersistence Utils library or not.
If you don’t want to disable only some specific types, then you can use the hypersistence.utils.types_contributor_filter
property, which can take the following values:
- a Java Object that implements the
Predicate
interface - a Java class that implements the
Predicate
interface - the fully-qualified name of the Java class that implements the
Predicate
interface
For instance, if you want to provide the Predicate
programmatically using the Spring Java-based configuration, you can do it like this:
protected void additionalProperties(Properties properties) { properties.put( HibernateTypesContributor.TYPES_CONTRIBUTOR_FILTER, (Predicate<UserType>) userType -> !(userType instanceof PostgreSQLInetType) ); }
The above Predicate
will disable the automatic registration only for the PostgreSQLInetType
from Hypersistence Utils.
Cool, right?
I'm running an online workshop on the 11th of October about High-Performance SQL.If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.
Conclusion
The automatic Type registration mechanism is very handy, especially on Hibernate 6, since you no longer have to specify the Hypersistence Utils Types manually for every entity property.
However, this automatic registration mechanism may also cause some unintended issues if a Hypersistence Utils Type replaces a custom Type that you planned on using. Being able to configure which Hypersistence Utils Types will be registered automatically will provide you more flexibility when deciding what Hibernate Type should be used to handle a certain DB column type or entity attribute Java type.
