Skip to content

Issue with spring-data "startingWith" and hibernate 5.6.7: Parameter value [\] did not match expected type [java.lang.String (n/a)]" with findAllByXXXStartingWith #2472

@glhez

Description

@glhez

Hello,

I am trying to fetch some entity using a “find all by property starting with” query which amount to a CriteriaQuery using javax.persistence.criteria.CriteriaBuilder.like(Expression, String, char)

When migrating to hibernate-entitymanager 5.6.7.Final I do have this error:

java.lang.IllegalArgumentException: Parameter value [\] did not match expected type [java.lang.String (n/a)]
	at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
	at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
	at org.hibernate.query.internal.QueryParameterBindingImpl.validate(QueryParameterBindingImpl.java:90) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
	at org.hibernate.query.internal.QueryParameterBindingImpl.setBindValue(QueryParameterBindingImpl.java:55) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
	at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:501) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
	at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:122) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
	at org.hibernate.query.criteria.internal.compile.CriteriaCompiler$1$1.bind(CriteriaCompiler.java:141) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
	at org.hibernate.query.criteria.internal.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:364) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
	at org.hibernate.query.criteria.internal.compile.CriteriaCompiler.compile(CriteriaCompiler.java:171) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
	at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:774) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
	at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:114) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]

It seems to happens when invoking twice the same method (in the attached demo_spring-data-2.6.3_hibernate_5.6.7..zip, findByLastNameStartingWith):

In the first invocation, we can see the validate (org.hibernate.query.spi.QueryParameterBindingValidator.validate(Type, Object, TemporalType)) takes the following arguments set:

  • null, '', null
  • org.hibernate.type.StringType, ‘Bauer%’, null

In the second case:

  • org.hibernate.type.StringType, '', null => FAILS (StringType expect a String, but got a Character)

The demo (was generated using Spring Initializer and this doc: https://www.baeldung.com/spring-data-jpa-query: demo_spring-data-2.6.3_hibernate_5.6.7..zip. It can be imported as Maven project in Eclipse and IntelliJ.

Note: I did try using CriteriaQuery as shown below and got no error and that's why I created issue here instead of hibernate, since I'm not sure where the problem might be (I would say that it is in Hibernate but given the example below works, there might be more to it):

    private static void demonstrateWithCriteria(EntityManager em) {
        CriteriaBuilder b = em.getCriteriaBuilder();
        CriteriaQuery<Customer> q = b.createQuery(Customer.class);
        q.where(b.like(q.from(Customer.class).<String> get("lastName"), b.literal("Bauer%"), '\\'));

        log.info("Test1 -- Customer found with criteria");
        em.createQuery(q).getResultList().forEach(bauer -> {
            log.info(bauer.toString());
        });

        log.info("Test2 -- Customer found with criteria");
        em.createQuery(q).getResultList().forEach(bauer -> {
            log.info(bauer.toString());
        });
    }

Versions information:

  • hibernate-core: 5.6.7.Final (tested also with 5.6.6.Final)
  • spring-data 2.6.2 / 2.6.3
  • java 1.8.0_322 (temurin / adoptium)
  • windows / linux (wsl)

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectFor an external project and not something we can fixstatus: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions