0

I have an app which scans a file once per day and persists entries from it to the database. But It may happen that when app scans file again, there could be still some entries scanned before and currently present in table. How to stop Hibernate from adding equal entries?

steve1337
  • 377
  • 1
  • 8
  • 3
    Database constraints is the answer. – Norbert Oct 13 '18 at 16:08
  • 1. You need to have unique keys in your database, so that adding duplicate entry can result in error and you can do what you have to do from there 2. You can have persistent list of DB entries and can check before adding new entries with that list – murthy Oct 13 '18 at 16:10
  • You can use Hibernate's @UniqueConstraint annotation. See https://stackoverflow.com/questions/15372654/uniqueconstraint-and-columnunique-true-in-hibernate-annotation –  Oct 13 '18 at 16:29
  • But what if i want the whole record to be unique, not just a single field? – steve1337 Oct 13 '18 at 16:30
  • Assuming you do not have an ID in the scanned file for each entry, you can add a unique column in your table which would be a hash (MD5 for example) of all of the entry data. That way, you only insert a new entry if there is no an existing entry with the same hash. – Sebastiandg7 Oct 13 '18 at 21:42
  • @SebastianDuque is it common solution? – steve1337 Oct 14 '18 at 08:38
  • 1
    @steve1337 The ideal situation would be to have an ID for each file entry. It is common to identify data by using integrity validation (hashing), so you can go safe with this solution. – Sebastiandg7 Oct 15 '18 at 03:01

0 Answers0