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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
glhez opened this issue Mar 22, 2022 · 27 comments
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid

Comments

@glhez
Copy link

glhez commented Mar 22, 2022

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)
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 22, 2022
@aspan
Copy link

aspan commented Mar 24, 2022

I have the same problem and i have debugged with different versions of hibernate. It works fine in hibernate 5.6.5 but fails in 5.6.6 or later.

Here is a minimal reproducer project:

demo.zip
.

As you can see the code runs fine on the first call but on the second call it fails. So there seems to be some caching issue somewhere.

        var people = personRepository.findByNameLike("%_1");
        assertEquals(1, people.size());
        // This is where it fails
        people = personRepository.findByNameLike("%_1");

The generated JPQL looks like this
For the first call

select generatedAlias0 from Person as generatedAlias0 where generatedAlias0.name like :param0 escape :param1

For all subsequent calls. Notice the escape :param0

select generatedAlias0 from Person as generatedAlias0 where generatedAlias0.name like :param0 escape :param0

@knoobie
Copy link

knoobie commented Mar 24, 2022

We faced the same problem updating spring boot to 2.6.5 where the updated version of hibernate is used by default.

Additionally we observed the problem with contains, startsWith and EqualsIgnoreCase as well.

@schauder
Copy link
Contributor

This seems to be a Hibernate problem.
I created an issue: https://hibernate.atlassian.net/browse/HHH-15142
And a reproducer: https://github.com/schauder/issue-HHH-15142 based on the reproducer of @aspan

@ettavolt
Copy link

I'd greatly appreciate if someone could help me promote the fix: hibernate/hibernate-orm#4918.

@fistons
Copy link

fistons commented Apr 3, 2022

A workaround would be to explicitly add @Param() for each argument in the repository methods (see #2479)

@THD-Thomas-Lang
Copy link

Another workaround seems to set the hibernate version (in Spring Boot´s pom.xml file) back to a working one:
<hibernate.version>5.6.5.Final</hibernate.version>

@4braincells
Copy link

workaround is fine ... but why depending on a buggy hibernate version when everyone updates spring boot to 2.6.6 because of vulnerabilty issues ... <:o(

@C-Otto
Copy link

C-Otto commented Apr 15, 2022

This is fixed in Hibernate 5.6.9 (according to https://hibernate.atlassian.net/browse/HHH-15142)

@basharataliv
Copy link

I got same issue but i resolve it by implementing the JPQL and did not use the 'startwith' function. Below is the example query
"Select c from Case c where c.name like :name%"

@fistons
Copy link

fistons commented Apr 25, 2022

Still have the issue with Spring Boot 2.6.7

@husniddinprogrammer
Copy link

#2472 (comment)

2 similar comments
@husniddinprogrammer
Copy link

#2472 (comment)

@husniddinprogrammer
Copy link

#2472 (comment)

@4braincells
Copy link

With gradle, just do this:

`implementation 'org.hibernate:hibernate-core:5.6.5.Final'

Should work with any spring (boot) version.
I think this had been said before...

@pankaj046
Copy link

Has hibernate 5.6.9 released yet?. I cannot find it in the main maven repo (https://mvnrepository.com/artifact/org.hibernate/hibernate-core), the latest version is 5.6.8

use spring boot 2.6.2 version

@gdisanto
Copy link

gdisanto commented May 2, 2022

Has hibernate 5.6.9 released yet?. I cannot find it in the main maven repo (https://mvnrepository.com/artifact/org.hibernate/hibernate-core), the latest version is 5.6.8

use spring boot 2.6.2 version

The project I am working is not using Spring boot, plus I do not want to downgrade my version of hibernate core.

@gregturn
Copy link
Contributor

gregturn commented May 3, 2022

Workaround is to either pin Hibernate to an older, working version:

<properties>
    <hibernate.version>5.6.5.Final</hibernate.version>
</properties>

...or if you don't want to go to an older version of Hibernate, to parameterize your repository...

@Repository
public interface BookRepository extends JpaRepository<Book, String> {

	List<Book> findByIsbnContaining(@Param("isbn") String isbn);
}

This forces query derivation to plugin the "right" parameter and hence not break at the Entity Manager.

@gregturn
Copy link
Contributor

gregturn commented May 3, 2022

Follow #2519.

@Muchomor
Copy link

5.6.9 version does not seem to resolve this problem...

Obcy added a commit to Obcy/zdjavapol111_shop_furniture_loft that referenced this issue May 18, 2022
@fistons
Copy link

fistons commented May 19, 2022

Sprint Boot 2.6.8 fixes the issue!

fred1878 added a commit to fred1878/Dark_Heresy_Character_Sheet_API that referenced this issue May 19, 2022
gregturn added a commit to spring-projects/spring-data-examples that referenced this issue May 19, 2022
Due to a bug in Hibernate (https://hibernate.atlassian.net/browse/HHH-15142), any Like-based custom finder will fail when submitted to the Entity Manager a second time.

This patch includes a workaround until Hibernate 5.6.9.Final is released.

See also: spring-projects/spring-data-jpa#2519, spring-projects/spring-data-jpa#2472

See #636.
rnjsrntkd95 added a commit to rnjsrntkd95/spring_ass_1_midas that referenced this issue May 27, 2022
쿼리스트링으로 전달된 검색 조건, 키워드를 기반으로 관리자 데이터를 조회함
하이버네이트 버그로 같은 URL 두번 요청 실패 발생, 하이버네이트 버전 변경
spring-projects/spring-data-jpa#2472
InnovateUK pushed a commit to InnovateUKGitHub/innovation-funding-service that referenced this issue Aug 1, 2022
peg implementation 'org.hibernate:hibernate-core:5.6.5.Final'
@mmm8955405
Copy link

I'd greatly appreciate if someone could help me promote the fix: hibernate/hibernate-orm#4918.

The hibernate-orm tools cannot report problems in github. They are inefficient!

They generate documentation and examples of DAOs. At present, it can only run normally under the ant plug-in and the corresponding life cyclerun. The hibernate tools maven cannot configure the DAO location, which is very difficult to use. And they are changing their name (jakarta). God. A bunch of bugs

@abhisheksangani
Copy link

abhisheksangani commented Dec 19, 2023

create a repository method using the Query annotation for your signature

rlarjsdud110 added a commit to rlarjsdud110/usedCar that referenced this issue Mar 5, 2025
- Hibernate 버전 문제를 해결하기 위해 build.gradle 수정 (버전 5.6.5.Final로 다운그레이드)
- 오류 관련 URL spring-projects/spring-data-jpa#2472
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests