Skip to content

Generic class parameter not being bound to query [DATACMNS-1002] #1452

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
spring-projects-issues opened this issue Mar 2, 2017 · 7 comments
Assignees
Labels
status: superseded An issue that has been superseded by another

Comments

@spring-projects-issues
Copy link

Michael Bragg opened DATACMNS-1002 and commented

Im in the process of upgrading our app and I've run into an issue related to changes made here to Parameter. We are upgrading from Spring Data Commons from 1.11.4.RELEASE to 1.12.x / 1.13.x. We have a hierarchical entity structure. e.g. ReasonLabel extends AbstractResolutionLabel. We have a table with a Discriminator Column for the name of the concrete type. We have the following query which previously worked:

@Query("SELECT label FROM AbstractResolutionLabel label WHERE label.group.id = :groupId AND label.providerType = :providerType AND TYPE(label) = :clazz")
<T extends AbstractResolutionLabel> List<T> findByGroupIdAndProviderTypeAndType(@Param("groupId") String groupId,
  @Param("providerType") ProviderType providerType, @Param("clazz") Class<T> clazz);

We are now getting the following error:

Caused by: org.hibernate.QueryException: Named parameter [clazz] not set
	at org.hibernate.query.internal.QueryParameterBindingsImpl.verifyParametersBound(QueryParameterBindingsImpl.java:234)
	at org.hibernate.query.internal.AbstractProducedQuery.beforeQuery(AbstractProducedQuery.java:1309)
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1408)
	at org.hibernate.query.Query.getResultList(Query.java:146)
	at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:121)
	at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:85)
	at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
	at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
	... 57 more

It appears that from this line the new isDynamicProjectionParameter(…) is returning true, so the class parameter is not being bound, because both the parameter type and return type (T) are both equal.

I appreciate this change has been here for some time now. This seems like a valid query that should be work. Many thanks


Affects: 1.13.1 (Ingalls SR1), 1.12.8 (Hopper SR8)

Reference URL: #150

Referenced from: pull request #199

@spring-projects-issues
Copy link
Author

Michael Bragg commented

Hi @olivergierke Does this look like a valid bug to you? Many thanks

@spring-projects-issues
Copy link
Author

Michael Bragg commented

Sugested minor change to make the dynamic projection parameter bindable. Thanks

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

Thanks for the report. Just a quick acknowledgement that I see this is a problem. I guess we didn't expect anyone to bind class values to parameters really. I don't think that using @Param to make this bindable is an option as the purpose of the annotation is a completely different one and mixing up the two concerns is gonna cause issues with people already using it and still expecting the usage of the parameter as projection.

I am still undecided on how to proceed here as all ways out imply some weird consequences for either side of the equation: people trying to use class as a projection input or people using it as plain parameter. Taking this back to the team to decide how to proceed

@spring-projects-issues
Copy link
Author

Michael Bragg commented

Thanks for the reply Oliver. Yes I've now realised that my small suggestion wouldn't be suitable. It would be possible to work around the issue, by creating dedicated repositories for the entities that extends the base entity for example (Something like this), but I'm still thinking it would be nice to have support for querying by type. e.g. Some sort of dedicated query keyword the maps to the discriminator somehow? Also are you aware or any offical documentation on how to use Sprint Data with entity hierarchies and the use of discriminator columns?

@rafg92
Copy link

rafg92 commented Jan 31, 2023

Hi, are there any updates about this issue?

@odrotbohm
Copy link
Member

odrotbohm commented Jan 31, 2023

Interestingly enough, this came up today in #2770. Working on a fix. A bit involved as it requires a few internal changes. We even thought about the option I previously ruled out (use @Param to force the parameter into binding) as a mitigating option. Could be interesting to only introduce this in the 2.x branch for folks that cannot upgrade to 3.0 just yet.

@rafg92 – what version are you currently working with? Would you be fine with a fix for this only added in 3.1, currently scheduled for release in mid April 2023 and inclusion in Boot 3.1 (to be released mid May 2023).

odrotbohm added a commit that referenced this issue Feb 1, 2023
…ection parameters.

This is a 2.7.x adapted fix for #1452 (the actual fix for 3.x contained in #2770). We temporarily support the use of @param on Class parameters to allow them to be be used as actual query parameters.

On 3.0.x the general parameter handling gets smarter so that this mitigation can be phased out pretty quickly, but this here seems to be a simple enough fix for those who cannot upgrade to 3.0 any time soon.

Fixes #1452.
@mp911de mp911de added status: superseded An issue that has been superseded by another and removed type: bug A general bug labels Feb 14, 2023
@mp911de
Copy link
Member

mp911de commented Feb 14, 2023

Fixed via #2770

@mp911de mp911de closed this as completed Feb 14, 2023
@mp911de mp911de added this to the 3.0.2 (2022.0.2) milestone Feb 14, 2023
mp911de pushed a commit that referenced this issue Feb 14, 2023
…ojection parameters.

This is a 2.7.x adapted fix for #1452 (the actual fix for 3.x contained in #2770). We temporarily support the use of @param on Class parameters to allow them to be be used as actual query parameters.

On 3.0.x the general parameter handling gets smarter so that this mitigation can be phased out pretty quickly, but this here seems to be a simple enough fix for those who cannot upgrade to 3.0 any time soon.

Closes #2770.
See #1452.
Original pull request: #2772
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

4 participants