-->
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.  [ 10 posts ] 
Author Message
 Post subject: many-to-many with additional attributes
PostPosted: Mon Aug 16, 2004 4:13 am 
Newbie

Joined: Mon Aug 16, 2004 3:49 am
Posts: 9
Location: Austria
Hibernate version: 2.1

Name and version of the database you are using: Firebird 1.5

hi,

i did some exercises with hibernate and i am content with the most functions of hibernate. today i want to tried an many-to-many association with additional attributes, as i had problems i tried to find answers in the documentation and here. but i still have a problem:

I have two classes: Employee (that is a subclass of person) and Publication. Between this 2 classes there is an association class Emp_Pub. I modelled this association class as it is told in the documentation, but now i have problems in creating objects - i think there is a fault in my java-class Emp_Pub

Person.hbm.xml
Code:
<class name="Person"
      table="PERSON"
      lazy="true">

   <id name="pId"
     type="long"
     column="PID">
     <generator class="native"/>
   </id>

...

   <joined-subclass name="Employee"
      table="EMPLOYEE"
      lazy="true">

      <key column="PID"/>

      <property name="education"
              type="string"
              column="EDUCATION"
              length="20"
              not-null="true"
              update="true"/>

         <many-to-one name="department"
             class="Department"
             cascade="all"
             not-null="true"
             column="DEPART_ID"
             update="true"/>

      
           <set name="publications" table="EMP_PUB">
            <key foreign-key="FK_EMPLOYEE_PID" column="EMPLOYEE_PID"/>

            <composite-element class="Employee_Pub">
              <property name="created" type="date"/>
             <many-to-one name="publication" foreign-key="FK_PUBLICATION_ID" column="PUBLICATION_PUBID" not-null="true"/>
            </composite-element>

   </joined-subclass>

   ...

</class>


Publication.hbm.xml
Code:
   <class name="Publication" table="PUBLICATION">
          <id name="pubId" column="PUBID" type="long">
                  <generator class="increment"/>
          </id>
          <property name="title" column="TITLE" type="string"/>
             <property name="publicated" column="PUBLICATED" type="boolean"/>
             <set name="employees"
             inverse="true"
             lazy="true"
             table="EMP_PUB"
                 cascade="all">
                 <key>
                     <column name="PUBLICATION_PUBID" not-null="true"/>
                 </key>
                 <many-to-many class="Employee">
                     <column name="EMPLOYEE_PID" not-null="true"/>
                 </many-to-many>
             </set>
        </class>


Employee_Pub.java
Code:
public class Employee_Pub {

    private Date created;
    private Employee employee;
    private Publication publication;

... getter & setter


now to my problem: how must the Employee_Pub.hbm.xml file musst looke like, i made some tries, but nothing worked.

thanks for helping, sorry that my english is not the best


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 10:29 am 
Newbie

Joined: Mon Aug 16, 2004 3:49 am
Posts: 9
Location: Austria
hm...

does anybody know a complete example of a many-to-many association with an additional attribute? i think this would be helpful for me, because i could only find fragments of of code. i would be very pleased!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 8:33 am 
Newbie

Joined: Mon Aug 16, 2004 3:49 am
Posts: 9
Location: Austria
i am really disappointed, that no one can or want to help me, i really do not know how to modell the xml-file of the association-class - i am also not sure how the java-file of this class has to be modelled - the two referencing objects (or only the primary keys of them?) and the additional attribut? or am i completly wrong?

as i told before: i miss a extensive example of a many to many relation with some additional properties. i can hardly not believe, that no other person had this problem before.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 8:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
understand "utilisateur" as "user"

utilisateur.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>   
<class name="XXX.Utilisateur" table="UTILISATEUR">      
<id column="LOGIN" name="login">      
<generator class="assigned"/>      
</id>      
<property column="NOM" name="nom" not-null="true" />      ...      
<bag name="roles" table="UTILISATEUR_ROLE">            
<key column="ID_UTILISATEUR"/>               
<composite-element class="XXX.UtilisateurRole">       
<property name="mail" />                                     
<many-to-one column="ID_ROLE" name="role" class="XXX.Role"/>    </composite-element>            
</bag>   
</class></hibernate-mapping>


role.hmb.xml
Code:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>   
<class name="com.auchan.balisage.bo.habilitation.Role" table="ROLE">
<id column="ID_ROLE" name="idRole">      
<generator class="sequence">         
<param name="sequence">SEQ_ROLE</param>   
</generator>
</id>
<property column="NOM" name="nom" not-null="false" />
                          ...   
</class>
</hibernate-mapping>


UtilisateurRole.hbm.xml
Code:
Fichier Role.hbm.xml :<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping>   
<class name= "XXX.UtilisateurRole" table="UTILISATEUR_ROLE">   <composite-id>
<key-many-to-one name="utilisateur" class="XXX.Utilisateur" column="ID_UTILISATEUR"/>     
<key-many-to-one name="role" classXXX.Role"column="ID_ROLE"/>    </composite-id>
<property name="mail" column="MAIL" />   
</class>
</hibernate-mapping>

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 12:34 pm 
Newbie

Joined: Mon Aug 16, 2004 3:49 am
Posts: 9
Location: Austria
Merci beaucoup anthony! now everything is clear and my testapplication works well!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 4:27 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you're welcome!
next time just be more patient ;)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 2:44 am 
Newbie

Joined: Wed Jul 07, 2004 4:38 am
Posts: 5
Hi, I got nearly the same problem, I understand your sample Anthony, but I'm getting a little bit lost when I tried to implement th userRole java file , specially I had some CGLIB probs and I'm not sure i overided equals() et hashcode() in the right manner. Could it be possible that you post your code of the java file?
Merci d'avance !
Sebi


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 5:58 am 
Newbie

Joined: Tue Aug 24, 2004 4:25 am
Posts: 14
I am facing the same problem as well. I would like to know how 3 classes look like. Thanks alot.


Top
 Profile  
 
 Post subject: This mapping doesn't work for schemaexport
PostPosted: Mon Sep 20, 2004 4:22 pm 
Newbie

Joined: Mon Mar 01, 2004 3:41 pm
Posts: 9
Hi anthony,

i was looking for a solution to this problem as well, and i tried your code. The classes compile fine. What happens to me when i do a schema export using the schemaexport Ant task, it fails. The message is:

Quote:
Schema text failed: net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property utilisateur in class XXX.UtilisateurRole


Do you know how to work around it?

Also, when looking at the classes, the Role class doesn't have a collection of Utilizateur. In the project i'm working on, i also need such a collection for the bidirectional relationship. i tried the <bag> mapping on the Role end and it doesn't work.

Would you please tell me whether there's a way to work this out?

Many thanks! i really appreciate your help!

celia


anthony wrote:
understand "utilisateur" as "user"

utilisateur.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>   
<class name="XXX.Utilisateur" table="UTILISATEUR">      
<id column="LOGIN" name="login">      
<generator class="assigned"/>      
</id>      
<property column="NOM" name="nom" not-null="true" />      ...      
<bag name="roles" table="UTILISATEUR_ROLE">            
<key column="ID_UTILISATEUR"/>               
<composite-element class="XXX.UtilisateurRole">       
<property name="mail" />                                     
<many-to-one column="ID_ROLE" name="role" class="XXX.Role"/>    </composite-element>            
</bag>   
</class></hibernate-mapping>


role.hmb.xml
Code:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>   
<class name="com.auchan.balisage.bo.habilitation.Role" table="ROLE">
<id column="ID_ROLE" name="idRole">      
<generator class="sequence">         
<param name="sequence">SEQ_ROLE</param>   
</generator>
</id>
<property column="NOM" name="nom" not-null="false" />
                          ...   
</class>
</hibernate-mapping>


UtilisateurRole.hbm.xml
Code:
Fichier Role.hbm.xml :<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping>   
<class name= "XXX.UtilisateurRole" table="UTILISATEUR_ROLE">   <composite-id>
<key-many-to-one name="utilisateur" class="XXX.Utilisateur" column="ID_UTILISATEUR"/>     
<key-many-to-one name="role" classXXX.Role"column="ID_ROLE"/>    </composite-id>
<property name="mail" column="MAIL" />   
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: i solved the problem myself
PostPosted: Thu Sep 23, 2004 2:26 pm 
Newbie

Joined: Mon Mar 01, 2004 3:41 pm
Posts: 9
Hi,

The problem with schema generation and bidiretional many-to-many issue was solved. Here's my code example:

Suppose a member can belong to many organizations, and one organization can have many members; the linking table has other properties defining the role of a particular member in a particular organization:

* In the mapping for the "member" table, i have this:
-------
<bag name="orgs" lazy="true" outer-join="true" table="member_org">
<key column = "member_no" />
<many-to-many class="com.blah.member.data.Organization" column="org_no" />
</bag>
-------

* Then in the mapping for the "organization" table, i have this:
---------
<bag name="members" lazy="true" inverse="true" outer-join="true" table="member_org">
<key column = "org_no" />
<many-to-many class="com.blah.member.data.Member" column="member_no" />
</bag>
---------

* Then in the mapping for the linking table with other properties, i have this:
---------
<composite-id>
<key-many-to-one name="member" class="com.blah.member.data.Member" column="member_no"/>
<key-many-to-one name="org" class="com.blah.member.data.Organization" column="org_no"/>
</composite-id>
<!-- plus other properties in the .hbm.xml file -->
---------

This generates everything nicely.

Thanks to everyone who contributed to my thinking!


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