Reputation: 863
my table has 3 columns: ID, City, District.
My web service give in input a list of couples (City, District). I need to avoid couples (City, District) duplicates.
How can I achieve this result? I can't use session.saveOrUpdate() because due to a different ID, all the records seems always different.
Please help me.
Thank you.
Upvotes: 1
Views: 1828
Reputation: 12998
The comment suggests what you should do
CONSTRAINT city_district_unique UNIQUE (city, district)
create a UNIQUE constraint for the fields. This can also be done with JPA annotations when you want to create the table with Hibernate
@Table(name = "yourtable", uuniqueConstraints = {@UniqueConstraint(columnNames = "subject"), ... })
When inserting, you should catch the Exception from the violation of the above constraint and handle it properly, like re-running the insert with changed parameters or ignore it at all. Even better, check your data befor it gets to Hibernate to avoid the invalid inserts beforehand.
Upvotes: 4