10

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:

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

Times 8 for each column.

I have defined these in my java code such:

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

which generated the error:

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

Final Answer

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

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;

2 Answers 2

12

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.

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

Sign up to request clarification or add additional context in comments.

2 Comments

You're right about Java's float being an approximation. I should have thought about using BigDecimal myself. Let me work on this. What would I have used in my DB to match to float? Also, it's MS SQL Server not MySql.
After changing to BigDecimal, I got: 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;
11

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

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

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

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

6 Comments

This is really a comment on another answer. I don't think it should be posted as a question.
@joelgoldstick I didn't mean to post it as question, but more as an answer. Tom mentioned this solution in a comment to the above answer. I personally saw this post 2 times before I realized the answer to his initial question was hidden in a comment to the answer above.
my mistake. I meand answer.
@andreico Thanks for the feedback. I have incorporated my answer into the question so that it's easier to find. Please upvote to raise the visibility.
This worked for my decimal(22,4) by doing precision=22, scale=4 for BigDecimal. Thanks man.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.