JPA merge() is removing duplicate row instead of throwing constraint violation
posted 2 years ago
I currently have 2 classes : AgencyDetail and AgencyBlockedDayDetail defines as follows:
Note that variables startDate and endDate are declared in superclass CalendarEvent.
And the AgencyDetail class is:
The problem is when I have two AgencyBlockedDayDetail objects i.e.
bdd1 with a reference to an AgencyDetail object agency01 and
bdd2 with a reference to an AgencyDetail object agency02.
I have the scenario where I update the AgencyDetail on bdd2 as follows:
At this point bdd1 and bdd2 are logically equivalent according to my equals() and hashCode() methods. However after the merge() runs in the update method of the service layer, bdd2 is removed/deleted completely. I would prefer a constraint violation exception to be thrown since attempting to save this would violate the unique constraint I have specified (i.e. startDate, endDate, agencyDetail).
Is this the correct behaviour? Can I get it to throw the constraint violation exception rather than just merging? This would be preferable as the user may have accidently entered in the wrong dates or agency detail.
An example of my jUnittest that fails is as follows: