2

So I got an error, string or binary data would be truncated error from sql-server. I am using hibernate to persist data to MS sql-server.

Usually when you get an error like this it means that the column you're trying to save to is not big enough to hold the data you are trying to insert.

I carefully started to compare to see if one of my BigDecimal variables in the domain doesn't have the correct precision, but after spending a lot of time, I realized this was not the case.

Turns out, I was missing @ManyToOne(optional = false) in the child domain. My parent domain object has a one to many relationship with the child. Adding the annotation solved the issue for me. And I am Hoping this will help somebody seeing similar error.

The question is: Why is database giving a data truncation error when ManyToOne annotation is missing in the domain?

2 Answers 2

3

Because since you haven't annotated your field, Hibernate applies the default mapping: treating the field as a serializable object that, once serialized, is stored in a binary column.

The length of the byte array resulting from the serialization was too large for the column, hence the error.

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

Comments

3

If no annotation is provided, Hibernate will apply the default mapping and will create a column with datatype tinyblob.

This will be storing the serialize data of that entity.

TINYBLOB: maximum length of 255 bytes.

Once the Entity is serialized, and data length exceeds 255 bytes, it will throw the error

data truncation error

2 Comments

Thank you ankur, but you wrote the same thing that JB wrote, so I'm choosing his answer over yours.
@pacman yes no issues, i just wanted to define the exact colummn datatype and its size, just to be more precise.

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.