This is basically a post relating to an issue logged
here. There hasn't been any progress made on the ticket in 4 years, so I thought I'd see if anyone has any advice.
The situation involves two entity objects, in our case Bond and Coupons. Bond has a reference to Coupons, with Cascade.ALL set. In an effort to not have a lot of obsolete information in our database, our code calls .delete() on Coupons objects that are no longer used.
Problem occurs when you've got two SessionFactory objects. Both have all the data cached in second level entity caches. One of them creates a new Coupons object, and makes the Bond refers to it, then cleans up by deleting the original Coupons object. The other creates a session, calls Session.get(Bond.class, id), then Session.refresh(bond) in an effort to get the latest content. Refresh blows up, because it tries to cascade the operation to the old Coupons object, which no longer exists in the database.
I would think that what refresh would do is first refresh the Bond object, then cascade the refresh to entities that are referenced in up-to-date version of the Bond. Instead, it does the reverse, cascading the refresh down to entities that are potentially not referenced any more, and perhaps no longer exist at all.
Right now the workaround we've come up with is to not delete obsolete Coupons objects. Then the refresh does more work than is strictly necessarily, but we do get the correct up-to-date contents for Bond and all it's referenced objects.
Is this in fact a bug, or are we thinking about .refresh() incorrectly?