Schorsch
Schorsch

Reputation: 323

Hibernate: SQL error 8152 - String or binary data would be truncated

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.

Upvotes: 5

Views: 19756

Answers (2)

Brad Turek
Brad Turek

Reputation: 2832

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.

Upvotes: 3

Schorsch
Schorsch

Reputation: 323

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.

Upvotes: 8

Related Questions