posted 5 years ago
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
bank.accounts.add(ae);
ae.banks.add(bank);
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().
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.