We have an application which needs to detect duplicates in certain fields on create. We are using Hibernate as our persistence layer and using Spring\'s HibernateTemplate.
i definitely go with Bozho answer. I think this is exactly the point.
There is a similar problem within the project i am currently working on. We are sharing informations between multiple servers based on filters and there can be scenarios where received objects already have been added to database so a PK exception would occur.
In our case these PK conflicts are very rare and so we consider this situation as exceptional. We are also using spring and hibernate and in order to seperate these concerns and due to Springs Transaction-Definitions we are using AOP to catch the specific DataIntegrity-Exception and rerun the Transaction doing the needed integrity checks on demand.
I could elaborate on this if you need help using the ExceptionHandlerAdvice.