Skip to content

Error binding partial countQuery parameters on @Query using nativeQuery and pagination #2587

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
bodiam opened this issue Jul 5, 2022 · 2 comments
Assignees
Labels
type: bug A general bug

Comments

@bodiam
Copy link

bodiam commented Jul 5, 2022

I have a native query with 3 parameters:

    @Query(nativeQuery = true)
    fun findNearbyDiveSpotsBySealifeContaining(@Param("latitude") lat: Double,
                                               @Param("longitude") lon: Double,
                                               @Param("objectId") objectId: String,
                                               pageable: Pageable): Page<SpotList>

In my orm.xml, I've created 2 queries for this. The data retrieval query works fine, but the count query doesn't:

    <named-native-query name="DiveSpot.findNearbyDiveSpotsBySealifeContaining.count" result-set-mapping="cntColumnResult">
        <query>
            SELECT count(*) as cnt
            FROM dive_spot u
                     JOIN dive_spot_sealife dss on u.id = dss.dive_spot_id
                     JOIN sealife s on dss.sealife_id = s.id
            WHERE s.object_id = :objectId
            -- I think this is a bug in spring-data-jpa
--             or s.latitude = :latitude   
--             or s.longitude = :longitude
       </query>
    </named-native-query>

I need the objectId parameter, but the lat/lng parameters I only need for the data query, so I don't use them here. But when I don't use them, I'm getting an exception:

Caused by: java.lang.IllegalArgumentException: Could not locate named parameter [latitude], expecting one of [objectId]
	at org.hibernate.query.internal.ParameterMetadataImpl.getNamedParameterDescriptor(ParameterMetadataImpl.java:229)
	at org.hibernate.query.internal.ParameterMetadataImpl.getQueryParameter(ParameterMetadataImpl.java:198)
	at org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:189)
	at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:501)
	at org.hibernate.query.internal.NativeQueryImpl.setParameter(NativeQueryImpl.java:650)
	at org.hibernate.query.internal.NativeQueryImpl.setParameter(NativeQueryImpl.java:66)
	at org.springframework.data.jpa.repository.query.QueryParameterSetter$BindableQuery.setParameter(QueryParameterSetter.java:326)
	at org.springframework.data.jpa.repository.query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.lambda$setParameter$4(QueryParameterSetter.java:117)
	at org.springframework.data.jpa.repository.query.QueryParameterSetter$ErrorHandling$1.execute(QueryParameterSetter.java:140)
	at org.springframework.data.jpa.repository.query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.setParameter(QueryParameterSetter.java:117)
	at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:82)
	at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:74)

As a workaround, I'm just doing something silly with the parameters, but could this be a bug?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 5, 2022
@gregturn gregturn added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 18, 2022
@darioseidl
Copy link

I'm also seeing this exception when I use a query parameter in the query, without using the same parameter in the count query for pagination.

I think this is the same issue as #2140.

@christophstrobl
Copy link
Member

thank you @darioseidl for linking the issues. True, both issues seem to have the same cause and should be closed by #3339

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants