Uploaded image for project: 'Spring Data JPA'
  1. Spring Data JPA
  2. DATAJPA-1564

Count query is faulty created when query have multiline select clause

    Details

      Description

      When SQL Query has multiline select clause, and it is a paging query, then the count query is created with a duplicate select clause

      @Query("""
      	select o.id,
      	o.name,
      	o.age
      	from User o
      	where o.age = :age
      """)
      fun findUserByAge(@Param("age") age: Int, pageable: Pageable): Page<User>
      

      The count query will be:

      select o.id,
      o.name
      o.age
      select count(o)
      from User o
      where o.age = :age

      To test it, add this test case to: src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java

      @Test
      public void createCountQuerySupportsNewLinesInSelectClause() {
      
      	assertThat(createCountQueryFor("select user.age,\n" + //
      					"  user.name\n" + //
      					"  from User user\n" + //
      					"  where user.age = 18\n" + //
      					"  order by user.name\n "), //
      			is("select count(user) from User user\n" + //
      					"  where user.age = 18\n "));
      }

        Attachments

          Activity

            People

            • Assignee:
              schauder Jens Schauder
              Reporter:
              mhewedy Muhammad
              Last updater:
              Mark Paluch
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: