Spring Boot 3 Data JPA – 本地查询中的错误数据类型映射

huangapple go评论65阅读模式
英文:

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 to Boolean instead of BigDecimal
  • NUMBER maps to Float instead of BigDecimal

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 = &quot;SELECT all_tab.column_name, all_tab.data_type, all_tab.DATA_LENGTH, all_tab.DATA_PRECISION, all_tab.DATA_SCALE&quot;
				+ &quot; FROM  all_tab_columns all_tab&quot;
				+ &quot; WHERE all_tab.column_name = :name&quot;, nativeQuery = true)
List&lt;Object&gt; findColumnDefinition(String name);

What I'm observing now, is that the mapping from DB types to Java types has changed:

  • NUMBER(1,0) maps to Boolean instead of BigDecimal

  • NUMBER maps to Float instead of BigDecimal

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 &lt;columnName&gt; with "CAST(&lt;columnName&gt; as INTEGER)". It's not perfect, but works. Looking for a cleaner solution though.

huangapple
  • 本文由 发表于 2023年4月6日 22:30:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定