Hi – I’m working on a web app. The items (plural because they’re a graph) a user sends may or may not already be in the database; they are deserialized from the payload, and at that point they’re detached.
More or less I can see that things work. My only gripe is that relationships with previous generation’s children don’t get broken. The only way I can tell they they were not part of the last payload is the version column. This is if I use saveOrUpdate().
If I try to merge into the session, and turn on orphan removal, and the use JPA persist(), I get the infamous exception about collections being new.
If I turn on orphan removal with saveOrUpdate(), it simply doesn’t work. I also have CascadeType.ALL on the OneToMany side.
How exactly can I make sure that OneToMany collections I (lazily or eagerly) load are actually the same thing I saved last time, without having to save them in the first place by rummaging through an existing graph?
Curiously, I see that Many-to-Many relationships do not act this way. You save without it, the relationship is broken. OneToMany still remain intact…
This is on 5.4.
Thanks everyone!