hibernatehibernate-criteria

Hibernate 6 uses wrong JdbcType for custom user type


I have a project that uses Quarkus 3.1.2.Final which uses hibernate-core in version 6.2.4.Final.

Here I have a custom user type for my custom class, which is used in an entity.

@Type(MyStringUserType.class)
public MyStringWrapper field;

When I execute a query with a condition that uses a function like upper, substring, etc. I get an exception.

Example

@Transactional
List<MyEntity> getByQuery() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MyEntity> query = cb.createQuery(MyEntity.class);
    Root<MyEntity> root = query.from(MyEntity.class);

    query.where(
            cb.equal(cb.upper(root.get("field")), new MyStringWrapper("SOME VALUE"))
    );
    TypedQuery<MyEntity> result = em.createQuery(query);
    return result.getResultList();
}

The exception:

[ERROR] org.acme.UserTypeTest.testQuery Time elapsed: 0.129 s <<< ERROR!

org.hibernate.QueryException: Parameter 1 of function upper() has type STRING, but argument is of type org.acme.MyStringWrapper

at org.hibernate.query.sqm.produce.function.ArgumentTypesValidator.throwError(ArgumentTypesValidator.java:253) at org.hibernate.query.sqm.produce.function.ArgumentTypesValidator.checkType(ArgumentTypesValidator.java:204) at org.hibernate.query.sqm.produce.function.ArgumentTypesValidator.validate(ArgumentTypesValidator.java:98) at org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor.generateSqmExpression(AbstractSqmFunctionDescriptor.java:104) at org.hibernate.query.sqm.function.SqmFunctionDescriptor.generateSqmExpression(SqmFunctionDescriptor.java:117) at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.upper(SqmCriteriaNodeBuilder.java:1541) at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.upper(SqmCriteriaNodeBuilder.java:182) at org.acme.UserTypeTestBean.getByQuery(UserTypeTestBean.java:34) at org.acme.UserTypeTestBean_Subclass.getByQuery$$superforward(Unknown Source) at org.acme.UserTypeTestBean_Subclass$$function$$1.apply(Unknown Source) [...]

When I do the same in Hibernate 5 it works fine.

I have two reproducers for this:

Is there any chance to get such a constellation running with Hibernate 6?

What I found so far by debugging

The called method org.hibernate.query.sqm.produce.function.ArgumentTypesValidator#getJdbcType returns a VarbinaryJdbcType, which then leads to (the throw of) an error in org.hibernate.query.sqm.produce.function.ArgumentTypesValidator#checkType as Varbinary is no character type.

Here I would expect, that a VarcharJdbcType is used, as in my custom user type the getSqlType returns java.sql.Types.VARCHAR.

I also tried to define the type using @JdbcType(VarcharJdbcType.class) and @JdbcTypeCode(Types.VARCHAR) but nothing changed.


Solution

  • As mentioned in HHH-16751, the problem should be fixed in version 6.3.0.