Skip to content

SubQuery & PageRequest : wrong alias used for sort #2518

Closed
@MarneusCalgarXP

Description

@MarneusCalgarXP

bug occurs in version : spring-data-jpa 2.6.4 (was ok in 2.6.3 and prior versions)

sample project to reproduce:

Model :

@Entity
public class Foo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

}

Repository :

public interface FooRepository extends JpaRepository<Foo, Long> {

    @Query("SELECT f" +
            " FROM com.example.demo.model.Foo f " +
            "WHERE f.id = ( " +
            "    SELECT MIN(g.id)" +
            "    FROM com.example.demo.model.Foo g " +
            " ) ")
    Page<Foo> findCustomFoo(Pageable pageable);

}

call :

Page<Foo> page = fooRepository.findCustomFoo(
	PageRequest.of(0, 10, Sort.Direction.ASC, "id")
);

HQL generated in versions prior to 2.6.3 (included)

SELECT f FROM com.example.demo.model.Foo f WHERE f.id = ( SELECT MIN(g.id) FROM com.example.demo.model.Foo g ) 
order by f.id asc

HQL generated in version 2.6.4

SELECT f FROM com.example.demo.model.Foo f WHERE f.id = ( SELECT MIN(g.id) FROM com.example.demo.model.Foo g ) 
order by g.id asc

==> the alias used in order by points to the subquery table, which is not available at global level

which leads to a QuerySyntaxException :

org.hibernate.hql.internal.ast.QuerySyntaxException: 
Invalid path: 'g.id' 
[SELECT f FROM com.example.demo.model.Foo f WHERE f.id = (     SELECT MIN(g.id)    FROM com.example.demo.model.Foo g  )  order by g.id asc]

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions