0

When trying to merge an entity with JPA (eclipselink implementation) it gets duplicated for every action on the containing entity. Why?

To make it more concrete: I've got a situation with in it parcels. If I'm adding one parcel everything is ok. If I add two parcels without leaving the view (with viewScoped backing bean) I'm getting 3 parcels.

How is that possible?

I'm using the merge method on situation to save the parcels added.

Nothing else is happening in the backingBean other than mergin.

GregD
  • 1,884
  • 2
  • 28
  • 55
  • 1
    You need to post code, not a description of what the code vaguely does. – Gimby Jul 30 '14 at 15:31
  • My guess is that code isn't actually really needed. I've also noticed that the ID from the `parcels` isn't filled after the merge is called although they are added to the databank right after the merge has been called from `situation`. – GregD Jul 30 '14 at 15:34
  • 1
    Your code doesn't do what you expect it to do, and then you guess that you don't need to post any code? – Gimby Jul 30 '14 at 15:37
  • I'm kind of sure you would know what's wrong reading the explanation only if you know what you're doing. Ah nvm, I'll figure it out anyway.. – GregD Jul 30 '14 at 15:45
  • 1
    I guess your code is super-secret. Good luck with that. – Gimby Jul 30 '14 at 16:14
  • Not at all actually. Just pretty sure the problem, and thereby the solution, would become clear just by reading the description and apparently it did for other people.. :) – GregD Jul 31 '14 at 12:19

1 Answers1

0

It's fixed by calling the create method on the parcels instead of calling the merge method on situation (that creates the parcels). So calling create on the entity instead of merge on the containing entity fixed it.

The merge on newSituation didn't work because of hashCode and equals problems. (see The JPA hashCode() / equals() dilemma)

Community
  • 1
  • 1
GregD
  • 1,884
  • 2
  • 28
  • 55