I have a user class with this property
@CollectionOfElements(fetch = FetchType.EAGER)
private Set<String> buddies;
I've chosen to use CollectionOfElements to avoid detachment problems and lazy initialization exception: this is a client server application where I have to continously exchange data and it was simpler to 'cut' the model a bit in order not to have too big graphs of object when moving things, or having to deal with lazy initialization exceptions and the like.
The problem is that I've encountered a bug with Hibernate and CollectionOfElements handling.
When I merge back an updated detached instance back to hibernate using EntityManager.merge, the instance gets correctly updated (the version property and simple properties are updated), but the linked tabled USERS_BUDDIES for the buddies CollectionOfElements property DO NOT GET UPDATED AT ALL.
Pseudo code:
// Load user (user with NO buddies)
User user = em.load(id);
// DETACH IT BY SENDING IT TO CLIENT
// Add a buddy
user.getBuddies().add(buddyId);
// SEND IT BACK TO SERVER
// merge updated entity with a buddy, back to server
em.merge(user);
RESULT: USERS_BUDDIES list is still empty, and when I load back the user after the session has been closed, I get back a user with the correct updated version, but with NO buddies.
Am I doing something wrong? Is this a known bug? How Can I avoid it, if possble?
I've tried deleting and re-adding the instance (remove/persist) but it doesn't work if user has constraints on it because of other tables.
Thanks in advance for any help
P.s. using both Postgresql and Derby, problems is the same with both DBs.
|