之前我用的是hibernate 5.x一切正常。
将 hibernate 更新到 6.1.7 后,项目出现异常。
我正在使用 springboot 3.0.5 并添加以下属性以在 mysql 8.0 上自动生成表:
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
我的实体如下:
@Data
@Entity
@Table(name = "T_TEST")
public class TPriceRange implements Serializable {
@Id
@Column(name = "ID", unique = true, nullable = false)
private Integer id;
@Column(name = "price", precision = 5, scale = 2)
private Double price;
}
当我启动项目时,异常(exception)是:
Caused by: org.hibernate.MappingException: Unable to determine SQL type name for column 'price' of table 't_test'
at org.hibernate.mapping.Column.getSqlTypeName(Column.java:252)
at org.hibernate.mapping.Column.getSqlType(Column.java:314)
at org.hibernate.tool.schema.internal.StandardTableExporter.getSqlCreateStrings(StandardTableExporter.java:99)
... 179 common frames omitted
Caused by: java.lang.IllegalArgumentException: scale has no meaning for floating point numbers
at org.hibernate.dialect.Dialect$SizeStrategyImpl.resolveSize(Dialect.java:3853)
at org.hibernate.dialect.MySQLDialect$1.resolveSize(MySQLDialect.java:108)
at org.hibernate.mapping.Column.getColumnSize(Column.java:358)
at org.hibernate.mapping.Column.getSqlTypeName(Column.java:248)
好像‘刻度’不能用,不知道为什么?
有人可以帮助我吗?
非常感谢!
hibernate6标度对于 float 没有意义
最佳答案
嗯,我的意思是,这个异常(exception)应该是不言自明的。
浮点类型没有刻度(这就是“浮点”的确切字面含义),因此这种映射不合理:
@Column(name = "price", precision = 5, scale = 2)
private Double price;
因此只需删除文本 scale = 2
即可。
如果您希望该货币值具有设定的精度和小数位数,则应使用 BigDecimal
而不是 Double
,并将其映射到 SQL 类型 DECIMAL (5,2)
或 NUMERIC(5,2)
。
只需删除文本 scale=2
即可。例如:
@Column(name = "price", precision = 5, scale = 2)
private BigDecimal price;
关于java - hibernate6抛出异常 'scale has no meaning for floating point numbers',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75862838/