Runtime Error when upgraded from java 11 to 17

165 views Asked by At

I'm getting this runtime error after upgrading java version from 11 to 17, hibernate version used is 6.3.0.CR1. The same code works fine with Java 11

Error at runtime:

Cannot invoke "org.hibernate.query.BindableType.resolveExpressible(org.hibernate.engine.spi.SessionFactoryImplementor)" because "declaredParameterType" is null
1

There are 1 answers

0
theodm On

We encountered the same error after upgrading from an older Hibernate version to Hibernate 6.2.13.FINAL, alongside a transition from javax.persistence to jakarta.persistence. While I can’t pinpoint the exact version that introduced the regression, the issue arose when preparing a named query:

var query = entityManager
        .createNamedQuery("...")
        ...
        .setParameter(
            "eingangsdatum_von",
            eingangsdatumVon != null ? Date.from(eingangsdatumVon) : null,
            TemporalType.TIMESTAMP
        )
        .setParameter(
            "eingangsdatum_bis",
            eingangsdatumBis != null ? Date.from(eingangsdatumBis) : null,
            TemporalType.TIMESTAMP
        );

In the past, invoking setParameter on javax.persistence.Query with a null value for the date was permissible. However, post-upgrade, this is no longer the case, resulting in the exception you’ve encountered.

As a workaround, we now invoke setParameter on jakarta.persistence.Query with the date parameter only when it’s non-null. For null values, we use the overloaded setParameter method that accepts an Object.

To streamline the modification of all relevant calls, we crafted the following utility class:


import jakarta.persistence.Query;
import jakarta.persistence.TemporalType;

import java.util.Date;

public final class HibernateQueryUtils {
    private HibernateQueryUtils() {

    }

    public static Query setParameter(Query query, String var1, Date var2, TemporalType var3) {
        if (var2 != null) {
            return query.setParameter(var1, var2, var3);
        }

        return query.setParameter(var1, null);
    }
}

And we utilize this method as follows:

HibernateQueryUtils
    .setParameter(
        query,
        "eingangsdatum_von",
        eingangsdatumVon != null ? Date.from(eingangsdatumVon) : null,
        TemporalType.TIMESTAMP
    );

HibernateQueryUtils
    .setParameter(
        query,
        "eingangsdatum_bis",
        eingangsdatumBis != null ? Date.from(eingangsdatumBis) : null,
        TemporalType.TIMESTAMP
    );

This approach effectively bypasses the issue, allowing us to handle null values appropriately while preparing our queries.