-->
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
PostPosted: Wed May 12, 2004 1:14 pm 
Newbie

Joined: Wed May 12, 2004 12:58 pm
Posts: 3
Hi!

I have a small problem with many-to-many association.

Situation: I have a User and Grp object and the many-to-many link is between these two objects.
Platform: Tomcat 5.0.18, JDK 1.4.2, Hibernate 2.1.3, Mysql 4.0.17 and Mysql J/Connection 3.0

Problem: I create an User object and I add Grp object to User (Grp is read from database), then I save the User.
Result: User object is stored to database but table that is used for many-to-many relation (usr_group) remains empty.

Mapping file:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping auto-import="true"
                   package="com.tahonen.domain">
   <class name="User" table="usr">
      <id name="id" unsaved-value="0">
         <generator class="native"/>
      </id>
      <property name="username" column="usr_nam"/>
      <property name="password" column="pwd"/>
      <property name="email" column="email"/>
      <property name="phone" column="phone"/>
      <property name="location"  column="loc"/>
      <property name="birthYear" column="birth_year"/>
      <property name="description" type="text"/>
      <set name="groups" table="usr_group" lazy="true" cascade="all">
          <key>
              <column name="usr_id" not-null="true"/>
          </key>
          <many-to-many class="Grp">
              <column name="grp_id" not-null="true"/>
          </many-to-many>
      </set>
   </class>
   <class name="Grp" table="grp">
      <id name="id" unsaved-value="0">
         <generator class="native"/>
      </id>
      <property name="name"/>
      <property name="created"/>
      <many-to-one name="owner" class="User" column="owner"/>
   </class>
</hibernate-mapping>


Java code:
Code:
. . .
Session s = HibernateUtil.currentSession();
User usr = new User();
usr.setUsername("username");
usr.setPassword("password");
usr.setCreated(new Date());
Set groups = new HashSet();
Grp g = ....//read grp from database
//I have tested that variable g is != null
groups.add(g);
usr.setGroups(groups);
s.save(usr);


I have also tested, that if Grp object that is added to User is not in db it's insterted to correct place, but still usr_group table remains empty

thanks,
tero


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.