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.

Parameter substitution error when limiting results on MS SQL Server and Hibernate Reactive 1.0.0.CR9

See original GitHub issue

Hello, I am encountering an issue with MS SQL query parameters in 1.0.0.CR9 when using Paging or Ranges (SQL Exception, message=‘Incorrect syntax near ‘?’.’), possibly because the parameter is in the Select clause?

Stack: Quarkus 2.1.0.Final / Panache Reactive / Mutiny 4.1.2 / Hibernate Reactive 1.0.0.CR9 / Hibernate Core 5.5.5.Final

From my rest resource, running this: [PanacheRepository].find("firstName LIKE ?1").page(Page.of(25)).list();

With SQL logging on in Hibernate, this is the produced SQL:

Hibernate:
    select
        top(?) request0_.id as id1_1_,
        request0_.caseId as caseid2_1_,
        request0_.caseNumber as casenumb3_1_,
        request0_.expires as expires4_1_,
        request0_.addressLine1 as addressl5_1_,
        request0_.addressLine2 as addressl6_1_,
        request0_.agency as agency7_1_,
        request0_.city as city8_1_,
        request0_.email as email9_1_,
        request0_.firstName as firstna10_1_,
        request0_.lastName as lastnam11_1_,
        request0_.phone as phone12_1_,
        request0_.relationship as relatio13_1_,
        request0_.state as state14_1_,
        request0_.zipcode as zipcode15_1_,
        request0_.submitted as submitt16_1_
    from
        Request request0_
    where
        request0_.firstName like @P1

Note the correct parameter substitution in the where clause, but substitution did not occur for the “top(?)” in the select clause. If I remove the paging and re-run, the query executes successfully.

Trace logging shown below, showing HQL and SQL. I would guess that Panache is using setMaxResults() on the query. Happy to open an issue with Quarkus or Mutiny if that’s where the issue is, and thank you in advance!

2021-08-05 12:55:57,342 DEBUG [org.hib.hql.int.ast.QueryTranslatorImpl] (vert.x-eventloop-thread-1) HQL: FROM myproject.entities.Request WHERE requestor.firstName LIKE ?1
2021-08-05 12:55:57,342 DEBUG [org.hib.hql.int.ast.QueryTranslatorImpl] (vert.x-eventloop-thread-1) SQL: select request0_.id as id1_1_, request0_.caseId as caseid2_1_, request0_.caseNumber as casenumb3_1_, request0_.expires as expires4_1_, request0_.addressLine1 as addressl5_1_, request0_.addressLine2 as addressl6_1_, request0_.agency as agency7_1_, request0_.city as city8_1_, request0_.email as email9_1_, request0_.firstName as firstna10_1_, request0_.lastName as lastnam11_1_, request0_.phone as phone12_1_, request0_.relationship as relatio13_1_, request0_.state as state14_1_, request0_.zipcode as zipcode15_1_, request0_.submitted as submitt16_1_ from Request request0_ where request0_.firstName like ?
2021-08-05 12:55:57,342 DEBUG [org.hib.hql.int.ast.ErrorTracker] (vert.x-eventloop-thread-1) throwQueryException() : no errors
2021-08-05 12:55:57,342 TRACE [org.hib.rea.ses.imp.ReactiveHQLQueryPlan] (vert.x-eventloop-thread-1) Find: FROM myproject.entities.Request WHERE requestor.firstName LIKE ?1
2021-08-05 12:55:57,344 TRACE [org.hib.eng.spi.QueryParameters] (vert.x-eventloop-thread-1) Named parameters: {1=%j%}
2021-08-05 12:55:57,345 TRACE [org.hib.typ.des.sql.BasicBinder] (vert.x-eventloop-thread-1) binding parameter [2] as [VARCHAR] - [%j%]
2021-08-05 12:55:57,345 TRACE [org.hib.loa.Loader] (vert.x-eventloop-thread-1) Bound [3] parameters total
2021-08-05 12:55:57,355 DEBUG [org.hib.SQL] (vert.x-eventloop-thread-1)
    select
        top(?) request0_.id as id1_1_,
        request0_.caseId as caseid2_1_,
        request0_.caseNumber as casenumb3_1_,
        request0_.expires as expires4_1_,
        request0_.addressLine1 as addressl5_1_,
        request0_.addressLine2 as addressl6_1_,
        request0_.agency as agency7_1_,
        request0_.city as city8_1_,
        request0_.email as email9_1_,
        request0_.firstName as firstna10_1_,
        request0_.lastName as lastnam11_1_,
        request0_.phone as phone12_1_,
        request0_.relationship as relatio13_1_,
        request0_.state as state14_1_,
        request0_.zipcode as zipcode15_1_,
        request0_.submitted as submitt16_1_
    from
        Request request0_
    where
        request0_.firstName like @P1
2021-08-05 12:55:57,358 ERROR [org.hib.rea.errors] (vert.x-eventloop-thread-1) HR000057: Failed to execute statement [$1select request0_.id as id1_1_, request0_.caseId as caseid2_1_, request0_.caseNumber as casenumb3_1_, request0_.expires as expires4_1_, request0_.addressLine1 as addressl5_1_, request0_.addressLine2 as addressl6_1_, request0_.agency as agency7_1_, request0_.city as city8_1_, request0_.email as email9_1_, request0_.firstName as firstna10_1_, request0_.lastName as lastnam11_1_, request0_.phone as phone12_1_, request0_.relationship as relatio13_1_, request0_.state as state14_1_, request0_.zipcode as zipcode15_1_, request0_.submitted as submitt16_1_ from Request request0_ where request0_.firstName like @P1]: $2could not execute query: java.util.concurrent.CompletionException: io.vertx.mssqlclient.MSSQLException: {number=102, state=1, severity=15, message='Incorrect syntax near '?'.', serverName='aa3ae4a0fd3c', lineNumber=1, additional=[io.vertx.mssqlclient.MSSQLException: {number=8180, state=1, severity=16, message='Statement(s) could not be prepared.', serverName='aa3ae4a0fd3c', lineNumber=1}]}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
bharwardcommented, Aug 6, 2021

@DavideD in addition to the test you proposed, may I also suggest an additional test like this:

       @Test
	public void testFirstResultZeroAndMaxResultsWithoutOrderBy(TestContext context) {
		test(
				context,
				openSession()
						.thenCompose( s ->
								s.createQuery( "from Flour" )
										.setFirstResult( 0 )
										.setMaxResults( 10 )
										.getResultList()
										.thenAccept( result -> {
											context.assertEquals( 3, result.size() );
										} )
						)
		);
	}

The difference is in removing the “order by”. The reason is that SQLServer2005LimitHandler writes the query as select top(?) ... whenever the query does not include an “order by”, but when it does it uses the offset ? fetch next ? syntax. So I think 2 test cases are needed.

Much appreciated!!

1reaction
DavideDcommented, Aug 6, 2021

Ah, you are right, this test will fail in HQLQueryParameterNamedLimitTest:

	@Test
	public void testFirstResultZeroAndMaxResults(TestContext context) {
		test(
				context,
				openSession()
						.thenCompose( s ->
								s.createQuery( "from Flour order by id" )
										.setFirstResult( 0 )
										.setMaxResults( 10 )
										.getResultList()
										.thenAccept( result -> {
											context.assertEquals( 3, result.size() );
										} )
						)
		);
	}

I will have a look

Read more comments on GitHub >

github_iconTop Results From Across the Web

1.0.0.CR10 Milestone - GitHub
Parameter substitution error when limiting results on MS SQL Server and Hibernate Reactive 1.0.0.CR9 bug Something isn't working.
Read more >
A brand new website interface for an even better experience!
Parameter substitution error when limiting results on MS SQL Server and Hibernate Reactive 1.0.0.CR9.
Read more >
https://www.lingexp.uni-tuebingen.de/z2/Morphology...
18-24 wristbands transact thatcham terse superconductivity presuming pre-match myosin moorgate loin hand-in-hand gymnasts drooping checksum 250.00 ...
Read more >
Parameter substitution error when limiting results on MS SQL ...
Hello, I am encountering an issue with MS SQL query parameters in 1.0.0.CR9 when using Paging or Ranges (SQL Exception, message='Incorrect syntax near...
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