Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA merge() is removing duplicate row instead of throwing constraint violation

 
neriyan nadasen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 jUnit test that fails is as follows:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic