Skip to content

JSqlParserQueryEnhancer errors with INSERT statements #2593

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

Closed
stulscott opened this issue Jul 13, 2022 · 5 comments
Closed

JSqlParserQueryEnhancer errors with INSERT statements #2593

stulscott opened this issue Jul 13, 2022 · 5 comments
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL type: bug A general bug

Comments

@stulscott
Copy link

When starting a Spring Boot application with >= 2.7.0 the application fails to start if it has native INSERT queries defined in JpaRepositories. The error is:

java.lang.ClassCastException: class net.sf.jsqlparser.statement.insert.Insert cannot be cast to class net.sf.jsqlparser.statement.select.Select

For example, any JpaRepository with the following will error and prevent the application from starting:

    @Modifying
    @Query(value = "INSERT INTO FOO(A) VALUES('A')", nativeQuery = true)
    void insertFoo();

detectParsedType() appears to neglect the Insert statement type:

			Statement statement = CCJSqlParserUtil.parse(this.query.getQueryString());

			if (statement instanceof Update) {
				return ParsedType.UPDATE;
			} else if (statement instanceof Delete) {
				return ParsedType.DELETE;
			} else if (statement instanceof Select) {
				return ParsedType.SELECT;
			} else {
				return ParsedType.SELECT;
			}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 13, 2022
@DiegoKrupitza
Copy link
Contributor

DiegoKrupitza commented Jul 14, 2022

Yeah you are totally right. When working on a344a76 I did not though about INSERT statements 🤦🏽‍♂️. Working on a PR that should fix that issue

DiegoKrupitza added a commit to DiegoKrupitza/spring-data-jpa that referenced this issue Jul 14, 2022
The `detectParsedType()` inside `JSqlParserQueryEnhancer` is now aware of `INSERT` statements, which means `INSERT` statements can now be used in native queries.

Closes spring-projects#2593
@gregturn gregturn self-assigned this Jul 18, 2022
@gregturn gregturn added type: bug A general bug in: query-parser Everything related to parsing JPQL or SQL and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 18, 2022
@gregturn gregturn added this to the 3.0 RC1 (2022.0.0) milestone Sep 23, 2022
@DaDom
Copy link

DaDom commented Oct 6, 2022

Hey @DiegoKrupitza and @gregturn !
Thanks a lot for working on this fix!
Will it be backported to 2.7.x, or only be available in version 3?

@tofdragon
Copy link

tofdragon commented Nov 24, 2022

Hey @DiegoKrupitza and @gregturn !

When will the 2.7.x release be fixed?

@DiegoKrupitza
Copy link
Contributor

Hey @DaDom & @tofdragon. Unfortunately I don't know if this will be backported to 2.7.x, since this is decided(?)/managed by the members of the spring-data-jpa team.
Since you already pinged @gregturn I think once he sees this and finds time to do it I see no reason why this should not happen.

There is currently a lot happening with QueryEnhancer so I think the next big change (#2639) will not be backported, so in the future it may be feasible to "upgrade". But this is just my personal opinion maybe the leads of this project have other plans.

Hope this somehow helps :)

gregturn pushed a commit that referenced this issue Dec 21, 2022
The `detectParsedType()` inside `JSqlParserQueryEnhancer` is now aware of `INSERT` statements, which means `INSERT` statements can now be used in native queries.

Closes #2593
@gregturn
Copy link
Contributor

Backported to 2.7.x.

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: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants