5

I am using Hibernate with MS SQL Server. When I try to store my entity bean, I keep getting: "SQL error 8152 sqlstate 22001" and "String or binary data would be truncated".

I haven't found this issue anywhere.

2 Answers 2

8

I finally solved it and wanted to share the solution here, in case anybody else has the same issue.

Normally this this occurs if a column is defined too small (like trying to store a string with 300 characters in a column defined as varchar(256)). But in my case it was different:

Solution: When I defined the table with annotations in hibernate, I defined a link to another table wrongly.

Instead of defining it as many-to-one:

@ManyToOne public myEntity getMyEntity() { return myEntity; } 

I defined it as a normal column:

@Column public myEntity getMyEntity() { return myEntity; } 

Maybe this helps anybody else.

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

1 Comment

This has saved me tons of time.
3

The cause of this, for me, was trying to put something too big into a column that can't fit it.

I thought the problem might be something else—nope—I was just checking the wrong column.

My advice is to

  1. Find an example that throws the error
  2. Go through and check the length of each of your columns against your example.

1 Comment

For me, I changed varchar(256) to text and solved problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.