spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
2.93k stars 1.39k forks source link

Unexpected Quoting on Column Name after Spring Boot 3 Migration #3466

Closed luic-c closed 1 month ago

luic-c commented 1 month ago

Hi, we are currently migrating from Spring Boot 2 to Spring Boot 3 and encountered the syntax error issue with ID$ field when trying to execute the data query function. We would like to know if this issue is expected or not:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "DLP1_0.id$" not found; SQL statement:
select dlp1_0."id$",dlp1_0.name from demo_log dlp1_0 where dlp1_0.name=?

It seems the problem lies on the additional quote for the ID$ field. This issue did not happen with Spring Boot 2.7, and trying to set globally_quoted_identifiers_skip_column_definitions=true also did not resolve this issue.

(Although trying to set hibernate.naming.physical-strategy to org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl did resolve the problem, but since we have other entities so I am not sure if this is the best approach...)

Additional info: Spring Boot starter JPA version: 2.7.14 -> 3.2.5 Hibernate version: 5.6.15 final -> 6.4.1 final Original database used: Oracle

demo.zip

quaff commented 1 month ago

It's a breaking change at Spring Boot side https://github.com/spring-projects/spring-boot/commit/4d30eb453f3d033784ca307257ae9436047bb54e

By default, Spring Boot configures the physical naming strategy with CamelCaseToUnderscoresNamingStrategy.

Spring Boot will lowercase column name in @Column() since v2.6.0, so the column name is "id$" in generated sql but ID$ in table structure, H2 treat quoted name as case sensitive, so there are different columns.

The workaround is changing ID$ to "id$" in your sql. Or set physical-strategy to org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl.

quaff commented 1 month ago

https://github.com/hibernate/hibernate-orm/commit/cbcec73d4fa4ca0845d0f8ca620dcdc02cd91b17#diff-f9280f07a52481f6f0ad138275022f60d985652522d975b0b7fa1519b9559e92R112

Hibernate will quote column name if it contains $ since 6.0.0.Beta2.

christophstrobl commented 1 month ago

Thank you @quaff for looking this up!