Skip to content
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

Use of Hibernate 5.3 and HibernateQuery results in "Could not locate named parameter" #2326

Closed
andygoossens opened this issue Jun 22, 2018 · 14 comments · Fixed by #2883
Closed
Projects

Comments

@andygoossens
Copy link

The use of querydsl-jpa's HibernateQuery with Hibernate 5.3 will thrown an exception when you use parameters:

java.lang.IllegalArgumentException: Could not locate named parameter [1], expecting one of []

	at org.hibernate.query.internal.ParameterMetadataImpl.getNamedParameterDescriptor(ParameterMetadataImpl.java:218)
	at org.hibernate.query.internal.ParameterMetadataImpl.getQueryParameter(ParameterMetadataImpl.java:187)
	at org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:188)
	at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:494)
	at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:107)
	at com.querydsl.jpa.hibernate.HibernateUtil.setValue(HibernateUtil.java:91)
	at com.querydsl.jpa.hibernate.HibernateUtil.setConstants(HibernateUtil.java:79)
	at com.querydsl.jpa.hibernate.AbstractHibernateQuery.createQuery(AbstractHibernateQuery.java:105)
	at com.querydsl.jpa.hibernate.AbstractHibernateQuery.createQuery(AbstractHibernateQuery.java:97)
	at com.querydsl.jpa.hibernate.AbstractHibernateQuery.fetch(AbstractHibernateQuery.java:174)
	at com.querydsl.example.jpa.repository.TweetRepository.findAllWithHibernateQuery(TweetRepository.java:30)
	at com.google.inject.persist.jpa.JpaLocalTxnInterceptor.invoke(JpaLocalTxnInterceptor.java:66)
	at com.querydsl.example.jpa.repository.TweetRepositoryTest.find_list_by_predicate(TweetRepositoryTest.java:44)

I am not the only person experiencing this error, as @jpimag experienced the same issue:
#2283 (comment)

To reproduce this issue, I modified the querydsl-example-jpa-guice example contained within the QueryDSL repository. You can find the patch here:
https://gist.github.com/andygoossens/5eed865ccfe84857f7e99ddb2dfb5790
Then just run test class com.querydsl.example.jpa.repository.TweetRepositoryTest and check whether it fails.

You can easily compare the different behaviour between Hibernate versions by altering the <hibernate.version> parameter in the POM file (part of the "hibernate5" profile). e.g. Hibernate version 5.2.17.Final works fine, but version 5.3.1.Final fails. Don't forget to activate the necessary Maven profile(s) of course! :-)

It looks like there is a mixup between named query parameters and positional query parameters. getConstantToLabel() could contain a constant with index "1" (as a String and meant to be used as ?1) but QueryDSL is calling Query<R> setParameter(String name, Object val); meant for named parameters (like :example). Hibernate does not find the named parameter "1" and fails.

@jpimag
Copy link

jpimag commented Jun 23, 2018

Thanks for your research.
I think this is due to a drop of legacy parameter support in hibernate 5.3
https://hibernate.atlassian.net/browse/HHH-12116

@Shredder121
Copy link
Member

Oh, so the recent Hibernate version did in fact not reestablish backwards compatibility?

@andygoossens
Copy link
Author

This issue is unrelated to #1917.

QueryDSL relied on Hibernate's behaviour that a positional parameter was basically a named parameter. This is no longer the case since 5.3.0.Beta1 (see the Hibernate ticket that @jpimag mentioned above).

From now on, QueryDSL needs to keep its positional parameters separated from the named parameters. Even though we have noticed this issue with Hibernate 5.3, a fix would also work with older versions. So in theory, there is no need to wait for QueryDSL 5.x and a patch could already be applied to QueryDSL's 4.x branches (obviously depends on whether the effort is worth it).

@andygoossens
Copy link
Author

Someone else discovered the same issue and worked around it:
Unimarket@7a7994c#diff-01d72489db0f2d9a9e2853fcc4d703a1
Unimarket@f7184a0#diff-6cf0d9d4e9431be92cccfbc859d0c1f0

That is not the proper fix as SerializerBase could have prefixed the number with a String, which will cause Integer.parseInt(key) to fail.

barrypitman added a commit to barrypitman/querydsl that referenced this issue Jul 16, 2018
… for treating numbered parameters as named parameters. see querydsl#2326 and https://hibernate.atlassian.net/browse/HHH-12116. Proper solution would be to modify SerializerBase to separate numbered and named parameters, as described here querydsl#2326 (comment), but this hacky solution is working for me
@PavlikPolivka
Copy link
Contributor

Hi.
Is somebody working on the fix for this?
We want to migrate to Hibernate 5.3 and we are hitting this error.
I can try to do the correct fix over the weekend if nobody is working on this.

@andygoossens
Copy link
Author

@PavlikPolivka: I don't think that anybody started creating a proper fix for it. Feel free to work on it.

@Shredder121
Copy link
Member

Could you test if the recent Hibernate versions?
I don't know what they've restored backwards compatibility with, but they did something.

@PavlikPolivka
Copy link
Contributor

PavlikPolivka commented Aug 11, 2018 via email

@PavlikPolivka
Copy link
Contributor

PavlikPolivka commented Aug 11, 2018

So. If I change this to number based parametrs it works ok with Hibernte 5.3.
If I run the test with Hibernate 4 I get
org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 2 that is strange. I definitely see it as parameter number 1. It works fine when I set it as string.

This means that if this fix is accepted there is incopatibility for hibernate 4.x.

PavlikPolivka added a commit to PavlikPolivka/querydsl that referenced this issue Aug 11, 2018
PavlikPolivka added a commit to PavlikPolivka/querydsl that referenced this issue Aug 11, 2018
PavlikPolivka added a commit to PavlikPolivka/querydsl that referenced this issue Aug 11, 2018
@m4urer
Copy link

m4urer commented Sep 28, 2018

What's going on with this? Is there going to be a new querydsl release with 5.3.x compatibility?
I was originally waiting for the fix for hibernate 5.2 ... now 5.3 is out ... is there a plan?
Querydsl is starting to feel a bit neglected ... is the idea to keep it alive and up to date on your part?
Thanks!

@Shredder121
Copy link
Member

Yes, of course it is. I'm just currently busy with other things that impact my future, as I've stated in multiple other threads, and the google group, when I have more time on my hands to work on my hobby I will.

@jfcabral
Copy link

@Shredder121 is there anything we can do to help sorting this out?
QueryDSL is such an useful framework! But I'm affraid that for Spring Boot users (v >=2.1), Hibernate 5.3 support is a must!
Thank you

@zvozin
Copy link

zvozin commented Jun 30, 2019

Is anyone working on, or planning to work on, patching this?

@idosal
Copy link
Member

idosal commented Feb 29, 2020

Fixed in #2354. It will be featured in the next official release, which will also remove support for Hibernate 4. For now, you can try it out in 4.2.3-SNAPSHOT. I'd appreciate your feedback. Thanks!

jwgmeligmeyling added a commit that referenced this issue Jun 4, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 4, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 4, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 4, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 4, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 4, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 4, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 4, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 4, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 4, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 7, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 7, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 7, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 7, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 7, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 7, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 7, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 7, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 7, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 7, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 8, 2021
jwgmeligmeyling added a commit that referenced this issue Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
5.x
  
Done
Development

Successfully merging a pull request may close this issue.

9 participants