0

I am trying to execute insert statement through hibernate hibernate.save(entity) to table.It gives error:

 SQL Error: 8152, SQLState: 22001
String or binary data would be truncated. 

When I try to execute query on sql editor it works fine but through hibernate.save(entity)its gives me above error.Can AnyOne please help me out whats the issue is?

1
  • what is the data type of the column that you are trying to insert? Commented Apr 14, 2017 at 13:27

3 Answers 3

1

Usually that issue is related to columns' type and length, check the length is big enough or the data type is compatible. That could be a binary string overflow when being populated to int (int32), etc.

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

5 Comments

how can i track down for the particular column?
do you have access to target server instance? if yes, go to the Tables of that server instance, find which table that you are trying to insert. You cannot work with table entity without checking the underlying tables
yes I have access to server and i know the table name in which I am trying to insert but all ididnt get from the error is the column name of that table on which there is either length or type problem. Also it works fine previously in postgresql but when i switched to sqlserver same query fails
You need to drill through the Entity, I suggest add breakpoint to where the insert fails, try to insert that record manually to table by using Insert statement to see what exactly happened.
Check that the data types are the same between the postgresql and sql server databases. Check that the numeric range is the same for the datatypes between the two databases if the field is a number. Databases don't always define things exactly the same way which is why changing database backend is almost always a problem and why smart database developers do not do it.
0

I already had this problem when using the wrong sql dialect. By example, i was using a mysql dialect with the sql server driver.

Bye

2 Comments

i am using following configration for SQL Server 2012: driver_class:net.sourceforge.jtds.jdbc.Driver dialect :org.hibernate.dialect.SQLServerDialect .. is it correct?
Yes, this is correct. But are you sure that this config is in the right place? Is the dialect in the jdbc.properties file? I don't remember exactly, but I think there was some things with the config places (one for the driver, on for the connection)
0

Finally Issue Resolved , Actually in my entity their is property :

@Column ( name = "myArray" )
private byte[] myArray;

so the table creation in SQL with type and size varBinary(255) which is short container.To fix it through hibernate Entity i added :

@Lob
@Column ( name = "myArray" )
private byte[] myArray;

thanks to :What is the significance of @javax.persistence.Lob annotation in JPA?

Comments

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.