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?

Noshaf
  • 207
  • 7
  • 21

3 Answers3

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.

LONG
  • 4,490
  • 2
  • 17
  • 35
  • how can i track down for the particular column? – Noshaf Apr 14 '17 at 13:32
  • 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 – LONG Apr 14 '17 at 13:33
  • 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 – Noshaf Apr 14 '17 at 13:37
  • 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. – LONG Apr 14 '17 at 13:40
  • 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. – HLGEM Apr 14 '17 at 14:36
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

  • i am using following configration for SQL Server 2012: driver_class:net.sourceforge.jtds.jdbc.Driver dialect :org.hibernate.dialect.SQLServerDialect .. is it correct? – Noshaf Apr 15 '17 at 20:33
  • 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) – Doublequote Apr 17 '17 at 08:03
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?

Community
  • 1
  • 1
Noshaf
  • 207
  • 7
  • 21