-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem with Many to Many mapping
PostPosted: Thu May 22, 2008 7:21 pm 
Newbie

Joined: Thu May 22, 2008 7:11 pm
Posts: 2
I have a many to many relationship between a User and Group object. I have mapped and coded it as shown below, but I when I try to add a user to group it fail - actually nothing happen. No error is thrown and nothing is written to the database.

Any thoughts on where the problem may be?

thanks
Scott


Maps:
Group Map


Code:
<hibernate-mapping>
    <class name="Group" table="GRP" >

        <comment></comment>
        <id name="grpId" type="string">
            <column name="GRP_ID" length="20" />
            <generator class="assigned" />
        </id>
        <property name="grpName" type="string">
            <column name="GRP_NAME" length="40">
                <comment></comment>
            </column>
        </property>
       
        <set name="users" inverse="true" table="USER_GRP" lazy="true" cascade="all-delete-orphan">
            <key>
                <column name="GRP_ID" length="20" not-null="true">
                    <comment></comment>
                </column>
            </key>
            <many-to-many entity-name="User">
                <column name="USER_ID" length="20" not-null="true">
                    <comment></comment>
                </column>
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>



User Map:

Code:
<hibernate-mapping>
    <class name="User" table="USERS" >
        <comment></comment>
        <id name="userId" type="string">
            <column name="USER_ID" length="20" />
               <generator class="assigned" />
        </id>
        <property name="firstName" type="string">
            <column name="FIRST_NAME" length="40">
                <comment></comment>
            </column>
        </property>
        <property name="lastName" type="string">
            <column name="LAST_NAME" length="40">
                <comment></comment>
            </column>
        </property>
           <set name="groups" table="USER_GRP" lazy="true">
            <key>
                <column name="USER_ID" length="20" not-null="true">
                    <comment></comment>
                </column>
            </key>
            <many-to-many entity-name="Group">
                <column name="GRP_ID" length="20" not-null="true">
                    <comment></comment>
                </column>
            </many-to-many>
        </set>
       
     </class>
</hibernate-mapping>



Java code in DAO:

Code:
public void addUserToGroup(Group group, User user) {
      log.debug("adding user to group");
      
      Set<User> userSet = group.getUsers();
      userSet.add(user);
      group.setUsers(userSet);
      
      try {
         sessionFactory.getCurrentSession().saveOrUpdate(group);
         log.debug("persist user to group successful");
      } catch (RuntimeException re) {
         log.error("persist failed", re);
         throw re;
      }
      
   }


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

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.