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: Simpliest many-to-many
PostPosted: Tue Dec 28, 2004 3:08 pm 
Newbie

Joined: Tue Dec 28, 2004 3:00 pm
Posts: 2
I'm trying to get many-to-many to work using the simpliest example possible: a group can have many users and a user can belong to many groups.
However, the association table does not get updated.

usertest.hbm.xml:
Code:
<hibernate-mapping>
   <class name="com.hibernatetest.model.UserTest" table="userTest">
      <meta attribute="class-description">
         Represents a record of a user
      </meta>

      <id name="id" type="int" column="usertest_id">
         <meta attribute="scope-set">protected</meta>
         <generator class="native"/>
      </id>
     
      <property name="userName" type="string"/>

      <set name="groups" table="usertest_grouptest" inverse="true" cascade="all">
         <key column="usertest_id"/>
         <many-to-many column="grouptest_id"
                       class="com.hibernatetest.model.GroupTest"
                       outer-join="true"/>
      </set>     
   </class>
   
   <class name="com.hibernatetest.model.GroupTest" table="groupTest">   
      <id name="id" type="int" column="grouptest_id">
         <meta attribute="scope-set">protected</meta>
         <generator class="native"/>
      </id>
     
      <property name="groupName" type="string"/>

      <set name="users" table="usertest_grouptest" inverse="true" cascade="all">
         <key column="grouptest_id"/>
         <many-to-many column="usertest_id"
                       class="com.hibernatetest.model.UserTest"
                       outer-join="true"/>
      </set>     
   </class>
</hibernate-mapping>


code:
Code:
      Configuration config = new Configuration();
      config.addClass(UserTest.class);
     
      sessionFactory = config.buildSessionFactory();

      GroupTest gr1 = new GroupTest();
      gr1.setGroupName("admin");     
      GroupTest gr2 = new GroupTest();
      gr2.setGroupName("normal");
     
      UserTest u1 = new UserTest();
      u1.setUserName("Joris Van den Bogaert");
      UserTest u2 = new UserTest();
      u2.setUserName("Alicia Kolesnikova");
     
      saveGroup(gr1);
      saveGroup(gr2);
     
      u1.setGroups(new HashSet());
      u2.setGroups(new HashSet());
     
      u1.getGroups().add(gr1);
      u2.getGroups().add(gr2);
     
      saveUser(u1);
      saveUser(u2);

...
   public static void saveUser(UserTest user) throws Exception {
      Session session = sessionFactory.openSession();
      try {
         session.save(user);
      } 
      finally {
         session.close();
      }     
   }
...
   public static void saveGroup(GroupTest group) throws Exception {
      Session session = sessionFactory.openSession();
      try {
         session.save(group);
      } 
      finally {
         session.close();
      }           
   }


While the USERTEST table and the GROUPTEST table get populated, the USERTEST_GROUPTEST always stays empty. I have tried different variations on the possible attributes, but no such luck.

I must be missing something fundamental?
Could someone help me out?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 28, 2004 3:24 pm 
Regular
Regular

Joined: Fri Aug 29, 2003 12:48 pm
Posts: 63
Could be wrong, but I believe you have to map one of the sets using inverse="false", thus making it the controlling collection. For that matter, I think the cascade attribute is meaningless when inverse="true".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 30, 2004 5:24 pm 
Newbie

Joined: Tue Dec 28, 2004 3:00 pm
Posts: 2
rhobot wrote:
Could be wrong, but I believe you have to map one of the sets using inverse="false", thus making it the controlling collection. For that matter, I think the cascade attribute is meaningless when inverse="true".


Thanks for your reply, I tried setting one of the sets inverse attribute to false, but unfortunately, I get the same results: the association table is not filled.

Any other ideas?

Thanks


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.