-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Issue with bi-directional association and session cache
PostPosted: Sat Aug 21, 2010 7:41 am 
Newbie

Joined: Sat Aug 21, 2010 7:21 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Issue with bi-directional association and session cache
PostPosted: Sat Aug 21, 2010 8:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
Group group = (Group) session.get("1");
user.getGroups().add(new Group());


This code creates a new group.... so it's not surprising if the group with id=1 is not associated with any user...

And... as you say, it's a bi-directional association, so you'll need to update both ends of the association. Read this chapter in the documentation: http://docs.jboss.org/hibernate/stable/ ... usingbidir

You code should be something like this:

Code:
User user = (User) session.get("1");
Group group = (Group) session.get("1");
user.getGroups().add(group);
group.getUsers().add(user);
session.flush();


No need to call session.save() since Hibernate will automatically detect changes and save to the database.


Top
 Profile  
 
 Post subject: Re: Issue with bi-directional association and session cache
PostPosted: Mon Aug 23, 2010 3:12 am 
Newbie

Joined: Sat Aug 21, 2010 7:21 am
Posts: 2
Thanks for fast reply!

I did a mistake when wrote
Code:
Group group = (Group) session.get("1");
user.getGroups().add(new Group());

Of course, there should have been
Code:
Group group = (Group) session.get("1");
user.getGroups().add(group);

Nevertheless, you understood and gave me advice.

One point that I found while investigating this is that updating both ends of the association is necessary to keep session cache sync. Right?
If I don't update second end of the association and clear session cache then on the next query I get all data linked fine - Hibernate stores and retrieve data correctly.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.