Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 262
Schorsch Avatar asked Mar 15 '23 11:03

Schorsch


2 Answers

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.

like image 150
Schorsch Avatar answered Apr 08 '23 05:04

Schorsch


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.
like image 22
Brad Turek Avatar answered Apr 08 '23 06:04

Brad Turek