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.
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().
An IDE is no substitute for an Intelligent Developer.
posted 2 weeks ago
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.
bacon. tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database