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.  [ 2 posts ] 
Author Message
 Post subject: many-to-many : i give up...:-(
PostPosted: Fri Jul 02, 2004 9:34 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
Hi, here is my pb that i can't understand :

2 class : contact and fonction with a n-n mapping that doesn't working

Can anyone help ?

My Fonction mapping :

Code:
<hibernate-mapping>
    <class
        name="framework.entreprise.Fonction"
        table="fonctions"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="long"
            unsaved-value="-1"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="libelle"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="libelle"
        />

        <property
            name="abreviation"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="abreviation"
        />

        <set
            name="contacts"
            table="fonctionscontacts"
            lazy="true"
            inverse="true"
            cascade="none"
            sort="unsorted"
        >

              <key
                  column="id_fonction"
              >
              </key>

              <many-to-many
                  class="framework.entreprise.Contact"
                  column="id_contact"
                  outer-join="auto"
               />

        </set>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Fonction.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>




My Contact mapping :

Code:
<hibernate-mapping>
    <class
        name="framework.entreprise.Contact"
        table="contacts"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="long"
            unsaved-value="-1"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="email"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="email"
        />

        <property
            name="fax"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="fax"
        />

        <property
            name="nom"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="nom"
        />

        <property
            name="prenom"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="prenom"
        />

        <property
            name="telephone"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="telephone"
        />

        <property
            name="portable"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="portable"
        />

        <many-to-one
            name="entreprise"
            class="framework.entreprise.Entreprise"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            column="id_entreprise"
        />

        <set
            name="offres"
            lazy="false"
            inverse="true"
            cascade="all-delete-orphan"
            sort="unsorted"
        >

              <key
                  column="id_contact"
              >
              </key>

              <one-to-many
                  class="framework.entreprise.OffreFret"
              />
        </set>

        <property
            name="identifiantInstranet"
            type="int"
            update="true"
            insert="true"
            access="property"
            column="identifiantInstranet"
        />

        <set
            name="fonctions"
            table="fonctionscontacts"
            lazy="true"
            inverse="true"
            cascade="none"
            sort="unsorted"
        >

              <key
                  column="id_contact"
              >
              </key>

              <many-to-many
                  class="framework.entreprise.Fonction"
                  column="id_fonction"
                  outer-join="auto"
               />

        </set>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Contact.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


My code :
Code:
Contact c=new Contact();
c.setId(-1);
c.setNom("Fabien");
session.save(c);
session.flush();
      
if (c.getFonctions()==null)   c.setFonctions(new HashSet());

Fonction f=new Fonction();
f.setId(-1);
f.setLibelle("fonction");
session.save(f);
session.flush();

c.getFonctions().add(f);
session.save(c);
session.flush();



The result :

Code:
Hibernate: insert into contacts (email, fax, nom, prenom, telephone, portable, id_entreprise, identifiantInstranet) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into fonctions (libelle, abreviation) values (?, ?)


Why my table fonctionscontacts is empty ?

Thanks,

Fabien.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2004 12:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
mm, because you have both sides of the association defined with inverse="true". One side needs to "manage" the association; this side should be marked as inverse="false".


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