英文:
Spring Boot 3 Data JPA - Wrong datatype mappings in native queries
问题
I've migrated an app from Spring Boot 2.x to 3.
With Spring Boot 3, JPA's data type mapping is behaving differently. I'm using native queries in some parts that return lists of objects. The Java types of these objects have now changed. This is with an Oracle DB.
For example:
@Query(value = "SELECT all_tab.column_name, all_tab.data_type, all_tab.DATA_LENGTH, all_tab.DATA_PRECISION, all_tab.DATA_SCALE"
+ " FROM all_tab_columns all_tab"
+ " WHERE all_tab.column_name = :name", nativeQuery = true)
List<Object> findColumnDefinition(String name);
What I'm observing now is that the mapping from DB types to Java types has changed:
NUMBER(1,0)
maps toBoolean
instead ofBigDecimal
NUMBER
maps toFloat
instead ofBigDecimal
Oracle JDBC driver documentation states that all NUMBER
types map to BigDecimal
.
Hibernate 6's documentation states this:
By default, Boolean attributes map to BOOLEAN columns, at least when the database has a dedicated BOOLEAN type. On databases which don't, Hibernate uses whatever else is available: BIT, TINYINT, or SMALLINT. ...
By default, Hibernate maps values of BigDecimal to the NUMERIC JDBC type.
So, what am I missing? Any ideas on what causes the change in type mappings or how I can get back the Spring Boot 2 behavior?
Edit:
I can rule out JDBC. I've made the same SQL queries with plain JDBC, and the data types are mapped correctly (NUMBER as BigDecimal)...
That would leave Hibernate 6 as the cause?
英文:
I've migrated an app from Spring Boot 2.x to 3.
With Spring Boot 3 JPA's data type mapping is behaving differently. I'm using native queries in some parts that return lists of objects. The Java types of these objects have now changed.
This is with an Oracle DB.
for example:
@Query(value = "SELECT all_tab.column_name, all_tab.data_type, all_tab.DATA_LENGTH, all_tab.DATA_PRECISION, all_tab.DATA_SCALE"
+ " FROM all_tab_columns all_tab"
+ " WHERE all_tab.column_name = :name", nativeQuery = true)
List<Object> findColumnDefinition(String name);
What I'm observing now, is that the mapping from DB types to Java types has changed:
-
NUMBER(1,0)
maps toBoolean
instead ofBigDecimal
-
NUMBER
maps toFloat
instead ofBigDecimal
Oracle JDBC driver doc state, that all NUMBER
types map to BigDecimal
.
Hibernate 6's docs state this:
> By default, Boolean attributes map to BOOLEAN columns, at least when the database has a dedicated BOOLEAN type. On databases which don’t, Hibernate uses whatever else is available: BIT, TINYINT, or SMALLINT. ...
> By default, Hibernate maps values of BigDecimal to the NUMERIC JDBC type.
So what am I missing? Any ideas what causes the change in type mappings or how I can get back the Spring Boot 2 behavior?
Edit:
I can rule out jdbc. I've made the same sqls with plain jdbc and the datatypes are mapped correctly (NUMBER as BigDecimal)...
That would leave hibernate 6 as the cause?
答案1
得分: 1
同样的错误在这里被发现:https://hibernate.atlassian.net/browse/HHH-16650
您需要更新您的Hibernate版本(6.2.4.Final或更新版本)。
英文:
Your same bug has been spotted here: https://hibernate.atlassian.net/browse/HHH-16650
You need to update your hibernate version (6.2.4.Final or newer)
答案2
得分: 0
我有一个类似的问题,整数被解释为tinyInt。解决方案是将 <columnName> 替换为 "CAST(<columnName> as INTEGER)"。这不是完美的,但可以使用。不过,我正在寻找更干净的解决方案。
英文:
I have a similar issue with Integer being interpreted as tinyInt. The solution was to replace <columnName> with "CAST(<columnName> as INTEGER)". It's not perfect, but works. Looking for a cleaner solution though.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论