Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kzander91 opened this issue May 24, 2023 · 3 comments
Closed

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

kzander91 opened this issue May 24, 2023 · 3 comments
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL type: regression A regression from a previous release

Comments

@kzander91
Copy link

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 24, 2023
@gregturn
Copy link
Contributor

gregturn commented May 24, 2023

I've reproduced the same thing using:

	@Test // GH-2977
	void isSubqueryThrowsException() {

		String query = """
				insert into MyEntity (id, col)
				select max(id), col
				from MyEntityStaging
				group by col
				""";

		assertThat(createQueryFor(query, Sort.unsorted())).isEqualTo(query);
	}

This reason this works with Spring Data JPA 3.0 is we didn't introduce the HQL parser until 3.1, so that switches to the old way.

@gregturn gregturn self-assigned this May 24, 2023
@gregturn gregturn added type: regression A regression from a previous release in: query-parser Everything related to parsing JPQL or SQL and removed status: waiting-for-triage An issue we've not yet triaged labels May 24, 2023
@gregturn gregturn added this to the 3.1.1 (2023.0.1) milestone May 24, 2023
gregturn added a commit that referenced this issue May 24, 2023
Potentialy subqueries can crop up inside INSERT statements. We need to handle this as well.

NOTE: INSERT statements aren't defined in JPQL, so this problem doesn't overlap with standard JPA queries.

See #2977
gregturn added a commit that referenced this issue May 24, 2023
Potentialy subqueries can crop up inside INSERT statements. We need to handle this as well.

NOTE: INSERT statements aren't defined in JPQL, so this problem doesn't overlap with standard JPA queries.

See #2977
gregturn added a commit that referenced this issue May 24, 2023
Potentialy subqueries can crop up inside INSERT statements. We need to handle this as well.

NOTE: INSERT statements aren't defined in JPQL, so this problem doesn't overlap with standard JPA queries.

See #2977
@gregturn
Copy link
Contributor

That's merged to main and backported to 3.1.x.

@kzander91
Copy link
Author

That was fast, thanks a lot :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: query-parser Everything related to parsing JPQL or SQL type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

3 participants