java Hibernate MS-SQL 发现错误的列类型:十进制,预期:浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7712598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:05:40  来源:igfitidea点击:

Hibernate MS-SQL Wrong column type Found: decimal, expected: float

javahibernate

提问by Thom

Note: Answer at end of question

注意:在问题末尾回答

I have created some columns in my My SQL database and have evidently not created floating point before because I had to look it up. For SQL compliance, I created my columns thus:

我在我的 SQL 数据库中创建了一些列,显然之前没有创建浮点数,因为我必须查找它。为了符合 SQL,我创建了我的列:

ALTER TABLE sensor ADD Field1_Calibration_Offset DECIMAL(4, 3);

ALTER TABLE sensor ADD Field1_Calibration_Offset DECIMAL(4, 3);

Times 8 for each column.

每列 8 次。

I have defined these in my java code such:

我在我的java代码中定义了这些:

@Column(name = "FIELD1_CALIBRATION_OFFSET") private Float field1CalibrationOffset;

@Column(name = "FIELD1_CALIBRATION_OFFSET") 私有浮动 field1CalibrationOffset;

which generated the error:

产生了错误:

Wrong column type in PsDb.dbo.Sensor for column FIELD1_CALIBRATION_OFFSET. Found: decimal, expected: float

PsDb.dbo.Sensor 中列 FIELD1_CALIBRATION_OFFSET 的列类型错误。发现:十进制,预期:浮点数

Final Answer

最终答案

Following @jbrookover's answer below, I changed to BigDecimal, I got:

按照下面@jbrookover 的回答,我改为 BigDecimal,我得到:

Wrong column type in PsDb.dbo.Sensor for column FIELD1_CALIBRATION_OFFSET. Found: decimal, expected: numeric(19,2)

So I looked up my hibernate mappings and did:

所以我查找了我的休眠映射并做了:

@Column(name = "FIELD1_CALIBRATION_SCALE", columnDefinition="decimal", precision=4, scale=3)
private BigDecimal field1CalibrationScale;

回答by jbrookover

I tend to let Hibernate create my tables, so I'm not 100% on this, but there is no reason to assume that Java's Float maps to MySQL's DECIMAL. In fact, according to the documentation, DECIMAL is an exact type while FLOAT and REAL are approximations. I believe Java's Float is an approximation, so this mismatch seems logical.

我倾向于让 Hibernate 创建我的表,所以我不是 100% 对此,但没有理由假设 Java 的 Float 映射到 MySQL 的 DECIMAL。事实上,根据文档,DECIMAL 是一个精确类型,而 FLOAT 和 REAL 是近似值。我相信 Java 的 Float 是一个近似值,所以这种不匹配似乎是合乎逻辑的。

If you want to use MySQL's DECIMAL, try Java's java.math.BigDecimalinstead of java.lang.Float.

如果您想使用 MySQL 的 DECIMAL,请尝试使用 Java 的java.math.BigDecimal而不是java.lang.Float.

回答by andreico

I've ran into this problem myself, using Jboss 7 and MSSQL.

我自己也遇到过这个问题,使用 Jboss 7 和 MSSQL。

I believe @The Thom's answer is correct and deserves more exposure.

我相信@The Thom 的回答是正确的,值得更多曝光。

please note that name parameter is optional if your value has the same name as the database column.

请注意,如果您的值与数据库列具有相同的名称,则 name 参数是可选的。

@Column(name="myColumnName" columnDefinition="decimal", precision=18, scale=3)
private BigDecimal myColumnName;