Dilip
Dilip

Reputation: 959

How to catch duplicate column value exception in Spring

I have written my DAO layer and service layer for my database using Spring.

In some of the tables we have unique constraints for some columns. If we try to insert the duplicate values for those columns, how do we catch those exceptions specifically based on the column name? Basically I need to know which column caused the query failure.

Upvotes: 2

Views: 1949

Answers (1)

skaffman
skaffman

Reputation: 403501

DAO operations which violate a unique key will throw a DataIntegrityViolationException. However, you won't easily be able to extract the specific column name from this, it's just not that detailed.

If you need this information, you should consider querying the database before you run the insert/update, checking that the constraint won't be violated. If that check fails, then you have your information.

Upvotes: 2

Related Questions