Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

TWO - records are getting saved In OneToMany Bidirectional Relation in Update Operation?

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to JPA, using JPA 2.0. I am facing on problem with OneToMany Bidirectional Relation. When i am updating these, TWO - records are getting saved. My Entities are:

1) Claimant: having relationship with its own as;

@ManyToOne(fetch=FetchType.EAGER, optional=true, cascade=CascadeType.DETACH)
private Claimant parentClaimant;
2)With ClaimantAddress:

@OneToMany(mappedBy="claimant", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private Set<ClaimantAddress> addresses = new HashSet<ClaimantAddress>();
3) and Other Side in ClaimantAddress:

private Claimant claimant;
and after updating these eneties I am saving this by using merge().

What is wrong here? I am not able to find out.
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if the object you are merging has two copies of the new object.

Also check if you are calling merge twice.

Also ensure you are maintaining both sides of the bi-directional relationship correctly.
Surendra Manikpuri
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James for reply;

Yes my merging Object has 2 Copies of the another object.
But That's our requirement... Our Claimant Object has address of two types : Personal and Seasonal and User can update any one OR both the addresses;

So if the above is the scenario then always two records will be saved OR is there any possibility is there that only one object of Claimant will be created and if not then when I am displaying this record I am Setting these values to DTO and taking both the address as a Single line But there i am getting two records with same ID for each of the address.

Is there any way to handle the above case or Need to make any Changes to my Entities ?? Please guide me so that i can resolve this issue


Surendra M
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic