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

JdbcSQLSyntaxErrorException after upgrade to 3.3.0 #40940

Closed
Sax388 opened this issue May 29, 2024 · 8 comments
Closed

JdbcSQLSyntaxErrorException after upgrade to 3.3.0 #40940

Sax388 opened this issue May 29, 2024 · 8 comments
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid

Comments

@Sax388
Copy link

Sax388 commented May 29, 2024

With the upgrade from Spring Boot 3.2.6 to 3.3.0 we're getting errors like these when running our tests on an H2 database:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "insert into report_config (created,exporter_type,file_name,period_type,tenant_id,updated) values (?,?,?,?,?,?) [*]returning id"; SQL statement:
insert into report_config (created,exporter_type,file_name,period_type,tenant_id,updated) values (?,?,?,?,?,?) returning id [42000-224]

This could potentially be connected to https://stackoverflow.com/questions/78539828/spring-data-jpa-validation-fails-after-update-to-spring-boot-3-3-0.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 29, 2024
@wilkinsona
Copy link
Member

I don't see the connection between the error that you're seeing and the question on Stack Overflow. The former is a problem reported by H2 when executing an SQL query. The latter is an error reported by Hibernate when parsing an HQL query.

If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label May 29, 2024
@quaff
Copy link
Contributor

quaff commented May 30, 2024

I believe you are using wrong hibernate dialect because H2Dialect will not generate unsupported sql like ... returning id.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 30, 2024
@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels May 30, 2024
@Sax388
Copy link
Author

Sax388 commented May 30, 2024

@quaff Indeed I'm using another dialect, as in production there's a postgresql database and I need it to be as close to this as possible. With Spring Boot 3.2.6 everything was fine. I followed the migration guide for Hibernate 6.5 but both resolutions found there still produce the error mentioned above. I got there through the release notes link.

This is the minimal sample you've asked for @wilkinsona , thank you for your swift and clear response!
https://github.com/Sax388/sql-h2-postgres-error-on-upgrade-to-spring-3.3.0

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 30, 2024
@wilkinsona
Copy link
Member

Thanks for the sample, I've reproduced the problem and observed that it does not occur when downgrading Hibernate to 6.4.8.Final:

ext['hibernate.version'] = "6.4.8.Final"

This tells us that the change in behavior is due to a change in Hibernate 6.5.

The SQL dialect for Postgres is generating different SQL and this SQL is now incompatible with H2 running in Postgres compatibility mode.

With Hibernate 6.5, the SQL is the following:

insert into report_config (period_type) values (?) returning id

With Hibernate 6.4, it's the following:

insert into report_config (period_type) values (?)

If it works fine with a real Postgres instance then this is arguably a bug in/limitation of H2's Postgres compatibility mode that Hibernate's changed SQL is triggering. If it doesn't work with a real Postgres instance then I think it's a Hibernate bug.

Either way, I'm afraid that this is out of Spring Boot's control and will have to be dealt with by the maintainers of Hibernate or of H2.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2024
@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels May 30, 2024
@quaff
Copy link
Contributor

quaff commented May 31, 2024

@quaff Indeed I'm using another dialect, as in production there's a postgresql database and I need it to be as close to this as possible. With Spring Boot 3.2.6 everything was fine. I followed the migration guide for Hibernate 6.5 but both resolutions found there still produce the error mentioned above. I got there through the release notes link.

This is the minimal sample you've asked for @wilkinsona , thank you for your swift and clear response! https://github.com/Sax388/sql-h2-postgres-error-on-upgrade-to-spring-3.3.0

@Sax388 Why not let Hibernate choose the right dialect? IMO, you shouldn't specify dialect if the database is well-known.

@Sax388
Copy link
Author

Sax388 commented May 31, 2024

@quaff Thank you for your support. I didn't quite get you. I intentionally chose to use

      driverClassName: org.h2.Driver
      url: jdbc:h2:mem:testdb;MODE=PostgreSQL;DEFAULT_NULL_ORDERING=HIGH;NON_KEYWORDS=VALUE

to make it behave more like the well-known database in production (there were some issues, not detected by the tests before). We're only using H2 for our tests. I think it's time to ditch it and move on to using a full postgresql database e.g. inside testcontainers. What do you think?

EDIT: I feel a little dumb now, but h2database/h2database#3962 (comment) told me there was a bad configuration right above the properties mentioned above. Without these it's actually working again.

@quaff
Copy link
Contributor

quaff commented May 31, 2024

@quaff Thank you for your support. I didn't quite get you. I intentionally chose to use

      driverClassName: org.h2.Driver
      url: jdbc:h2:mem:testdb;MODE=PostgreSQL;DEFAULT_NULL_ORDERING=HIGH;NON_KEYWORDS=VALUE

to make it behave more like the well-known database in production (there were some issues, not detected by the tests before). We're only using H2 for our tests. I think it's time to ditch it and move on to using a full postgresql database e.g. inside testcontainers. What do you think?

EDIT: I feel a little dumb now, but h2database/h2database#3962 (comment) told me there was a bad configuration right above the properties mentioned above. Without these it's actually working again.

Sorry, I thought you are manually set hibernate dialect, It's MODE=PostgreSQL in jdbc url make hibernate detect H2 as PostgreSQL, I would like suggest you use H2 for unit tests and introduce testcontainers to use real PostgreSQL in integration tests.

EDIT: you are manually set hibernate dialect indirectly by spring.jpa.database=postgresql, not caused by MODE=PostgreSQL.

@guilhermec-costa
Copy link

Thanks for the sample, I've reproduced the problem and observed that it does not occur when downgrading Hibernate to 6.4.8.Final:

ext['hibernate.version'] = "6.4.8.Final"

This tells us that the change in behavior is due to a change in Hibernate 6.5.

The SQL dialect for Postgres is generating different SQL and this SQL is now incompatible with H2 running in Postgres compatibility mode.

With Hibernate 6.5, the SQL is the following:

insert into report_config (period_type) values (?) returning id

With Hibernate 6.4, it's the following:

insert into report_config (period_type) values (?)

If it works fine with a real Postgres instance then this is arguably a bug in/limitation of H2's Postgres compatibility mode that Hibernate's changed SQL is triggering. If it doesn't work with a real Postgres instance then I think it's a Hibernate bug.

Either way, I'm afraid that this is out of Spring Boot's control and will have to be dealt with by the maintainers of Hibernate or of H2.

@wilkinsona really appreciate your help. I was getting the exact same error on trying to insert data into a user table like the following:
insert into users (email, login, password, user_role) values (?, ?, ?, ?) returning id;

The problem was the "returning id". I just downgraded hibernate and overwrote the version from the one installed from spring jpa. It worked. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

5 participants