Skip to content

NPE in HqlQueryTransformer.isSubquery for INSERT from SELECT HQL #2977

@kzander91

Description

@kzander91

With 3.1.0, repository initialization fails with

Caused by: java.lang.NullPointerException: Cannot invoke "org.antlr.v4.runtime.ParserRuleContext.getParent()" because "ctx" is null
	at org.springframework.data.jpa.repository.query.HqlQueryTransformer.isSubquery(HqlQueryTransformer.java:100) ~[spring-data-jpa-3.1.0.jar:3.1.0]

when declaring the following named query:

insert into MyEntity (id, col)
select max(id), col
from MyEntityStaging
group by col

Entities:

@Entity
@NamedQuery(name = "MyEntity.copyFromStaging", query = """
        insert into MyEntity (id, col)
        select max(id), col
        from MyEntityStaging
        group by col""")
class MyEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String col;
}

@Entity
class MyEntityStaging {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String col;
}

Repository:

interface MyEntityRepository extends JpaRepository<MyEntity, Long> {
    @Modifying
    @Transactional
    void copyFromStaging();
}

Reproducer:
demo.zip
Extract and run ./mvnw spring-boot:run to observe the exception.
In the pom.xml, change the spring-data-jpa.version from 3.1.0 to 3.0.6 and run again, now it works.

Metadata

Metadata

Assignees

Labels

in: query-parserEverything related to parsing JPQL or SQLtype: regressionA regression from a previous release

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions