• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Q on ManyToMany mapping when I update a parent with a new child it replaces the existingchild  RSS feed

Ranch Hand
Posts: 630
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have three tables BANK, ACCOUNT BANK_ACCOUNT

When I add a BANK say with bankId 123, and ACCOUNT with accountId 1234. In my table BANK_ACCOUNT I have 123,1234 which is correct
When I update BANK bankkId with a new ACCOUNT accountId 1235. In my table BANK_ACCOUNT I have 123,1235. The mapping 123,1234 has been removed.

What I should happen is 123,1234

For Bank Entity I have

Im my account Entity I have

For saving I use save from  

Thanks for any help.
Saloon Keeper
Posts: 20510
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a little confusing since you're somewhat casual with what name you use to refer to what, but I think I follow it.

Question: when you add the new account to the bank, do you do this:

AccountEntity ae = new AccountEntity();
//... stuff to initialize ae

You have to use "add" to ensure that the new account doesn't simply replace the existing one. Also, of course, check to make sure that the existing account was fetched before adding the new one. If you're doing a lazy fetch or a detached operation, you risk losing sync on the data if the fetch wasn't done first.

And while in some cases you don't have to explicitly make both links, it's always safer to. For one thing, if you don't the in-memory entity map won't be correct, even if the database itself takes the hint.

Also note that when you persist out a set of records, the return may not be literally the same set of records that you persisted. The records might compare ".equals()", but not "==". So always use the returned records, especially after a merge().
Tony Evans
Ranch Hand
Posts: 630
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim, thanks for replying.

You mentioned a fetch, so that is where I have been going wrong.

What I have is a bank bankId 1234 with an account 1234.   So in my table for BANK_ACCOUNTI have 1234,1234

What I was doing was creating a new bank object with bankId 1234  with a new account 1235 and saving that object. I thought the JPA ManyToMany mapping would handle the merging for me.

But what you are saying is that what I need to do is
Fetch Bank for bankId 1234, and then fetch the Account collection, and add the new account .
Then save the bank entity.

Thank you.

Regards Tony.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!