How to implement EnhancedUserType

Hello Hibernate People,

I implemented a custom user type to map boolean values to database values. When I execute a query with the following criteria, I get an exception, that tells me that the user type cannot be casted to EnhancedUserType:

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
...
criteriaBuilder.isTrue(root.get("imported"))

The imported property is a boolean value mapped with this user type.

However, when I change the criteria using equal() instead of isTrue() it works:

criteriaBuilder.equal(root.get("imported"), true))

So I have the following questions:

  1. Why do I need to implement EnhancedUserType when I use isTrue()?
  2. Is there an advise how to implement the interface EnhancedUserType correctly?
  3. Two methods of this interface are deprecated. Does this mean, that this methods are not called by Hibernate anymore and may return null?
  4. I found some articles, which implement the method objectToSQLString() so that it returns the string value enclosed in single quotes. Is this the right way to do it?

Unfortunately the JavaDocs of this interface is not very helpful.

Kind regards
Thomas Mayr

1 Like