-->
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: problem with many-to-many bidirectional association
PostPosted: Wed Jun 30, 2010 3:22 am 
Newbie

Joined: Wed Jun 30, 2010 3:06 am
Posts: 2
Hi,
I've got a problem with a many-to-many bidirectional association with a join table.
When I get the set<Camera> from the object Prenotazione everything is ok, but when a I get the set<Prenotazione> from the object Camera I obtain an empty set (size=0). I define in Camera.java a set<Prenotazione> and in Prenotazione.java a set<Camera>.
I can't understand why. Does anybody help me, please?


These are my *.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
<class name="it.sistemaPrenotazioni.bid.camera.Camera" table="CAMERA">
<composite-id>
<key-property name="numCamera"/>
<key-many-to-one name="struttura" class="it.sistemaPrenotazioni.bid.struttura.Struttura" column="struttura" />
</composite-id>
<property name="numLetti" column="numLetti"/>
<property name="tipo" column="tipo"/>
<property name="descrizione" column="descrizione"/>
<set name="camerePrenotate" inverse="true" table="RELATIVA" >
<key>
<column name="camera"/>
<column name="struttura"/>
</key>
<many-to-many column="prenotazione" class="it.sistemaPrenotazioni.bid.prenotazione.Prenotazione" />
</set>
<set name="costaTariffa" table="COSTA" lazy="false">
<key>
<column name="camera"/>
<column name="struttura"/>
</key>
<many-to-many column="tariffa" class="it.sistemaPrenotazioni.bid.Tariffa" />
</set>

<joined-subclass name="it.sistemaPrenotazioni.bid.cameraFumatori.CameraFumatori" extends="it.sistemaPrenotazioni.bid.camera.Camera" table="CAMERAFUMATORI">
<key>
<column name="camera"/>
<column name="struttura"/>
</key>
</joined-subclass>

</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
<class name="it.sistemaPrenotazioni.bid.prenotazione.Prenotazione" table="PRENOTAZIONE" abstract="true">
<id name="codPrenotazione" column="codPrenotazione">
<generator class="native"></generator>
</id>
<property name="tariffaTotale" type="double" column="tariffaTotale"/>
<property name="dataInizio" type="date" column="dataInizio"/>
<property name="dataFine" type="date" column="dataFine"/>
<property name="dataRegistrazione" type="date" column="dataRegistrazione"/>

<many-to-one name="operatore" column="operatore" class="it.sistemaPrenotazioni.bid.Operatore" lazy="false" cascade="all"/>
<many-to-one name="cliente" column="cliente" class="it.sistemaPrenotazioni.bid.cliente.Cliente" lazy="false"/>

<set name="camerePrenotazione" table="RELATIVA" lazy="false">
<key>
<column name="prenotazione"/>
</key>
<many-to-many class="it.sistemaPrenotazioni.bid.camera.Camera">
<column name="camera" />
<column name="struttura" />
</many-to-many>
</set>

<joined-subclass name="it.sistemaPrenotazioni.bid.prenotazioneAnnullata.PrenotazioneAnnullata" extends="it.sistemaPrenotazioni.bid.prenotazione.Prenotazione" table="PRENOTAZIONEANNULLATA" >
<key column="prenotazione"/>
<property name="motivo" not-null="false"/>
<property name="dataAnnullamento"/>
<many-to-one name="operatore" column="operatore" class="it.sistemaPrenotazioni.bid.Operatore" lazy="false" />
</joined-subclass>

<joined-subclass name="it.sistemaPrenotazioni.bid.prenotazioneConfermata.PrenotazioneConfermata" extends="it.sistemaPrenotazioni.bid.prenotazione.Prenotazione" table="PRENOTAZIONECONFERMATA" lazy="false">
<key column="prenotazione" />
<many-to-one name="cartaCredito" column="cartaCredito" class="it.sistemaPrenotazioni.bid.CartaCredito" />
</joined-subclass>

<joined-subclass name="it.sistemaPrenotazioni.bid.prenotazioneRichiesta.PrenotazioneRichiesta" extends="it.sistemaPrenotazioni.bid.prenotazione.Prenotazione" table="PRENOTAZIONERICHIESTA">
<key column="prenotazione" />
</joined-subclass>

</class>
</hibernate-mapping>

And this is the script for the creation of the join table

CREATE TABLE Relativa (
prenotazione mediumint unsigned not null,
camera numeric(2) unsigned not null,
struttura varchar(30) not null,
primary key(prenotazione, camera, struttura),
CONSTRAINT relativa_ibfk_1 FOREIGN KEY (camera, struttura) REFERENCES Camera(numCamera, struttura),
CONSTRAINT relativa_ibfk_2 FOREIGN KEY (prenotazione) REFERENCES Prenotazione(codPrenotazione)
) ENGINE = InnoDB ;


Top
 Profile  
 
 Post subject: Re: problem with many-to-many bidirectional association
PostPosted: Sat Jul 03, 2010 6:57 pm 
Newbie

Joined: Tue Mar 30, 2010 8:45 pm
Posts: 2
try adding inverse="false" in Camera mapping.

Check out my sample code

Code:

<class name="User">
              <list name="roles" table="tbl20User_Role" cascade="all"
         lazy="true" inverse="false">
         <cache region="cache.role" usage="read-write" />
         <key>
            <column name="UID" not-null="true" />
         </key>
         <list-index column="role_index" base="0"></list-index>
         <many-to-many class="domain.Role" column="roleid">
         </many-to-many>
      </list>
</class>



Code:
<class name="Role">
                <list name="users" table="tbl20User_Role" lazy="true" inverse="true" cascade="all">
         <cache region="cache.user" usage="read-write" />
         <key>
            <column name="roleid" not-null="true" />
         </key>
         <list-index column="user_index" base="0"></list-index>
         <many-to-many class="domain.User" column="UID">
         </many-to-many>

      </list>
   </class>


I am not sure. Let me know if this helps. And Correct me if I am wrong.


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.