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?

- 220
- 2
- 4
- 14

- 245
- 1
- 7
- 18
5 Answers
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?

- 16,038
- 10
- 74
- 104

- 118,147
- 33
- 203
- 236
-
here i am using Spring hiernate Annotations, and maitained relationship between the tables, i kept ManytoOne relation with fetch mode as EAGER. here one point is when i am working on same table with some other operation like, i am getting the data of table and making some modification when i am submiting, the previous whole data has to be deleted and the new data what i am modified is going to store. this task was running successfully. in this case i can also append new row in the page. – Naresh Dec 10 '10 at 15:20
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.

- 41
- 1
DataIntegrityViolationException can also be violating a unique constraint on the database (i.e. a alternate key). Check, if your inserted data rows contain duplicate data that is violating such a unique constraint.

- 621
- 7
- 13
-
Question about this. Is it a good practice to handle duplicated validations from db and not from the app itself? – jscherman Aug 04 '15 at 18:30
-
it can be both, but only DB knows it's duplicate or not at that time. How would you handle validation in app without db? – Andrii Plotnikov Jun 30 '16 at 07:56
I treat DataIntegrityViolationException in ExceptionInfoHandler, finding DB constraints occurrences in root cause message and convert it into i18n message via map. See code here: https://stackoverflow.com/a/42422568/548473

- 1
- 1

- 16,647
- 10
- 125
- 197
if you import database from somewhere then you have to take data folder of server too , and change this data folder to your server's data folder .

- 198
- 2
- 5