Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we solve the DataIntegrityViolationException in Spring Hibernate?

I am using Spring Hibernate with HibernateSupportDao, when I try to send multiple rows of data to store in my db, it raises a DataIntegrityViolationException at a particular row. The same scenario has worked in remain in task on the same table. I do not get the problem even when I use the same code for the current task. Why was a DataIntegrityViolationException thrown and how can I rectify that?

like image 212
Naresh Avatar asked Dec 10 '10 14:12

Naresh


2 Answers

Directly from the API:

Exception thrown when an attempt to insert or update data results in violation of an integrity constraint. Note that this is not purely a relational concept; unique primary keys are required by most database types.

Basically it means that Hibernate tried to do something, but the database raised an exception. Most common case might be that you are deleting a parent, but not deleting a child with a foreign key to that parent.

What are your transitive persistence (i.e. cascade) settings?

like image 162
hvgotcodes Avatar answered Sep 18 '22 14:09

hvgotcodes


When you encounter this exception when selecting some fields from the table, then the reason could be the length of the one of the fields in the select statement. check for all the select field lengths and compare it with the table description. any field would have violated the field restriction.

like image 43
sampath Avatar answered Sep 19 '22 14:09

sampath