-->
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.  [ 5 posts ] 
Author Message
 Post subject: Many-to-many relationship query
PostPosted: Mon Sep 29, 2003 7:01 am 
Newbie

Joined: Mon Sep 29, 2003 6:47 am
Posts: 7
Hi.

I have a model where Users can be in many Groups, Users should have a Collection of Groups that they're in, and Groups should have a Collection of Users which are a member of the current group.

Here is my mapping document:
Code:
   <class name="com.foo.User" table="USERS">
        <id name="id" type="long" unsaved-value="null" column="id">
            <generator class="native"/>
        </id>

        <property name="name" type="string"/>

        <property name="password" type="string"/>

        <property name="email" type="string"/>

        <set name="groups" lazy="true" table="GROUP_USERS">
            <key column="user_id"/>
            <many-to-many class="com.foo.Group" column="group_id"/>
        </set>

    </class>

    <class name="com.foo.Group" table="GROUPS">
        <id name="id" type="long" unsaved-value="null" column="id">
            <generator class="native"/>
        </id>

        <property name="name" type="string"/>

        <property name="description" type="string"/>

        <set name="users" lazy="true" table="GROUP_USERS">
            <key column="group_id"/>
            <many-to-many class="com.foo.User" column="user_id"/>
        </set>

    </class>


I create 2 users and 2 groups, and add both users to both groups:

Code:
        User user1 = new User();
        user1.setName("user1");
        user1.setPassword("password");
        user1.setEmail("user1@foo.com");
        sess.save(user1);

        User user2 = new User();
        user2.setName("user1");
        user2.setPassword("password");
        user2.setEmail("adam@foo.com");
        sess.save(user2);

        Group group1 = new Group();
        group1.setName("test group 1");
        group1.setDescription("test group 1 - description");
        sess.save(group1);

        Group group2 = new Group();
        group2.setName("test group 2");
        group2.setDescription("test group 2 - description");
        sess.save(group2);

        Query groupq = sess.createQuery("from com.foo.Group group");
        List groups = groupq.list();
        for (int i = 0; i < groups.size(); i++) {
            Group group = (Group) groups.get(i);
            log.debug("Found group: " + group.getName());

            Set userSet = group.getUsers();

            if (null == userSet) {
                userSet = new HashSet();
            }

            Query userq = sess.createQuery("from com.foo.User user");
            List users = userq.list();

            for (int j = 0; j < users.size(); j++) {
                User user = (User) users.get(j);
                log.debug("Adding user: " + user.getName() + " to group: " + group.getName());
                userSet.add(user);
            }

            group.setUsers(userSet);
            sess.saveOrUpdate(group);
            sess.flush();
        }


Now in another method, using the same Hibernate Session:

Code:
        Query userq = sess.createQuery("from com.foo.User user");
        List users = userq.list();
        for (int i = 0; i < users.size(); i++) {
            User user = (User) users.get(i);
            Set groups = user.getGroups();
            if (null == groups) {
                log.debug("user: " + user.getName() + " does not seem to be in any groups");
            }
            else {
                for (Iterator iterator = groups.iterator(); iterator.hasNext();) {
                    Group group = (Group) iterator.next();
                    log.debug("user: " + user.getName() + " is in group: " + group.getName());
                }
            }
        }


And the User never seems to have a Set which contains the Group's that the User is in.

Any ideas?

-Ian.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2003 8:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I'm not 100% sure, but it looks like you don't understand Hibernate APIs very well.

* the saveOrUpdate() call is not required - Hibernate implements automatic dirty checking
* the flush() call should certainly NOT be done inside the loop!
* doesn't look like you are committing the transaction


Perhaps it would help to re-read the Hibernate manual carefully.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2003 10:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I think this is a database isolation issue. The methods do different operations
thus they should be in different transactions. Load the data in a transaction and commit it. This will allow the database to collect the data in the second method (it would normally not as the database would be running in a strict isolation level rather than read-not-commited). Also, don't use flush in a loop as you have. Flush only when you need to which in this case its after the loop.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2003 10:27 am 
Newbie

Joined: Mon Sep 29, 2003 6:47 am
Posts: 7
Thanks very much for the pointers, I'll be reviewing the documentation shortly.

I understand there are many ways in which hibernate can be used, I just want to make sure we get it right.

Are there any members of the development team interested in contacting me with regards to development consultation? If so mail me off list.

-Ian.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2003 10:39 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 10:32 pm
Posts: 35
Location: Toronto, Ontario
I had a similar problem until I put inverse=true on the users set in the Group mapping. In the User mapping the set groups has inverse=false.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.