question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

List params are not expanded in native queries

See original GitHub issue

The query below is not expanded into for the list and an exception is thrown

  @Query(value = "select t.* from hcp_tenant t " +
            "join hcp_tenant_consumer tc on tc.tenant_id = t.id and tc.topic_id in :topicIds",
            nativeQuery = true)
    Set<HcpTenantDTO> findByConsumersTopicsIdInList(List<Long> topicIds);

I have tried (:topicIds) as well but same result as the expansion isn’t happening

SELECT hcp_topic_.`id`,hcp_topic_.`name`,hcp_topic_.`partitions`,hcp_topic_.`config`,hcp_topic_.`email`,hcp_topic_.`ms_id`,hcp_topic_.`compatibility`,hcp_topic_.`aha_id` FROM `hcp_topic` hcp_topic_ INNER JOIN `hcp_org` hcp_topic_hcp_org_ ON hcp_topic_.`hcp_org_id`=hcp_topic_hcp_org_.`id` INNER JOIN `hcp_env` hcp_topic_hcp_org_env_ ON hcp_topic_hcp_org_.`env_id`=hcp_topic_hcp_org_env_.`id` WHERE (hcp_topic_hcp_org_env_.`id` = ?)
10:09:13.254 [pool-2-thread-7] DEBUG io.micronaut.data.query - Executing Query: select t.* from hcp_tenant t join hcp_tenant_consumer tc on tc.tenant_id = t.id and tc.topic_id in ?
10:09:13.290 [pool-2-thread-7] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: Unable to set PreparedStatement value: Parameter index out of range (2 > number of parameters, which is 1).
io.micronaut.data.exceptions.DataAccessException: Unable to set PreparedStatement value: Parameter index out of range (2 > number of parameters, which is 1).
	at io.micronaut.data.jdbc.mapper.JdbcQueryStatement.newDataAccessException(JdbcQueryStatement.java:273)
	at io.micronaut.data.jdbc.mapper.JdbcQueryStatement.setLong(JdbcQueryStatement.java:139)
	at io.micronaut.data.jdbc.mapper.JdbcQueryStatement.setLong(JdbcQueryStatement.java:33)
	at io.micronaut.data.runtime.mapper.QueryStatement.setDynamic(QueryStatement.java:134)
	at io.micronaut.data.jdbc.mapper.JdbcQueryStatement.setDynamic(JdbcQueryStatement.java:92)
	at io.micronaut.data.jdbc.mapper.JdbcQueryStatement.setDynamic(JdbcQueryStatement.java:33)
...

Caused by: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
	at com.mysql.cj.jdbc.ClientPreparedStatement.checkBounds(ClientPreparedStatement.java:1402)
	at com.mysql.cj.jdbc.ClientPreparedStatement.getCoreParameterIndex(ClientPreparedStatement.java:1415)
	at com.mysql.cj.jdbc.ClientPreparedStatement.setLong(ClientPreparedStatement.java:1610)
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setLong(HikariProxyPreparedStatement.java)
	at io.micronaut.data.jdbc.mapper.JdbcQueryStatement.setLong(JdbcQueryStatement.java:137)
	... 127 common frames omitted


Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
Deniz97commented, Oct 9, 2020

I am facing the same problem:

@Query("SELECT DISTINCT b.* " +
         "FROM  (SELECT * " +
         "            FROM category " +
         "            WHERE id IN (:ids) ) as a " +
         "join category as b on a.parent_id = b.id or a.id = b.id ")
  List<Category> selectWithParents(Collection<Long> ids);

io.micronaut.data.exceptions.DataAccessException: Unable to set PreparedStatement value: Invalid value "2" for parameter "parameterIndex" [90008-200]

Any updates on this?

Btw the value for ids was [2, 4] in this case.

0reactions
cemocommented, Dec 27, 2020

Would you give a try to fix regex @zhuravskiy? 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing List<Integer> in spring data jpa native query
A JPQL query allows the JPA provider to create the SQL it needs dynamically based on the list passed in, so it can...
Read more >
DataReadQuery List parameter for IN clause — oracle-tech
Lists will not be expanded on when using native SQL as it would require parsing and manipulating the SQL string passed in.
Read more >
Define and Call Data Queries - ForgeRock Backstage
Define and Call Data Queries. An advanced query model enables you to define queries and to call them over the REST or Resource...
Read more >
Running parameterized queries | BigQuery - Google Cloud
Note that a query can use positional or named parameters but not both. ... Construct the value as a comma-separated list of elements...
Read more >
Bash Reference Manual - GNU.org
is not removed. The special parameters ' * ' and ' @ ' have special meaning when in double quotes (see Shell Parameter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found