Hello,
I Have got an issue with bi-directional association and session cache.
Situation is easy for understanding.
There are two entities:
Code:
User {
Set<Group> groups;
}
Group {
Set<User> users;
}
I execute operation which should attach user to some group and then
check that user was attached to group (in one transaction):
Code:
// pseudo code
// attach user
User user = (User) session.get("1");
Group group = (Group) session.get("1");
user.getGroups().add(new Group());
session.save(user);
session.flush();
// get group
Group groupWithUser = (Group) session.get("1");
// get user previously attached
// this returns 0 !!!
System.out.pringln(groupWithUser.getUsers().size());
All this (attaching user to group) is in single transaction.As far as I understand session cache after saving user contains 2 entities: user with filled group list and group with empty user list. So that's why I have empty user list in group.
How to avoid this situation? Of course I can clear cache before getting group, but is there other solution?
I don't put mapping, because suppose that it's clear enough and it's better to discuss this in more abstract way to determine the best approach to solve this.
Thanks in advance.