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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: many-to-many - not by PK column
PostPosted: Wed Apr 13, 2011 9:35 am 
Newbie

Joined: Wed Apr 13, 2011 8:58 am
Posts: 11
Hi,
I didn't find the way how to map the relation manyTomany, but not by PK column.
All exemples for mapping the relation many-to-many is done based on PK column.
I have a table Card(ID_CARD PK, CARD_NR)
and table Promo( ID_PROMO PK, ...)
The rows from column CARD_NR are distincts.

So I want that in my intermediar table which represents the relation between those tables to be
PromoCard (ID_PROMO, CARD_NR)

The Cards.hbm.xml contains this mapping
<set name="promos" table="PROMO_CARD" cascade="save-update" lazy="true">
<key column="CARD_NR" />
<many-to-many column="ID_PROMO" class="Promo"/>
</set>

The Promos.hbm.xml contains this mapping
<set name="cards" table="PROMO_CARD" cascade="save-update" lazy="true">
<key column="ID_PROMO"/>
<many-to-many column="CARD_NR" class="Card"/>
</set>
When I'm trying to save a set of cards using a promo, the table PROMO_CARD will contains in the column the value of ID_CARD.
Can you tell me, please, where am I wrong?
Thank you.


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Wed Apr 13, 2011 11:08 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
You need to specify a property-ref in your many-to-many mapping


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Thu Apr 14, 2011 4:03 pm 
Newbie

Joined: Wed Apr 13, 2011 8:58 am
Posts: 11
Thank you very much for your answer.
But many-to-many doesn't use property-ref, no? I searched on the internet and I didn't find anywhere property-ref using in many-to-many relation. :(


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Thu Apr 14, 2011 4:21 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
According to the jboss doc you can use a property-ref in a many-to-many

http://docs.jboss.org/hibernate/stable/ ... s-ofvalues


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Fri Apr 15, 2011 4:01 am 
Newbie

Joined: Wed Apr 13, 2011 8:58 am
Posts: 11
I tried to use it in my many-to-many relation, but isn't work well. It's inserting records only PK's values (ID_PROMO, ID_CARD) in the table PROMO_CARD .

The Cards.hbm.xml contains this mapping

<property name="cardSerialNr" unique="true" column="CARD_NR"/>
<set name="promos" table="PROMO_CARD" cascade="save-update" lazy="true">
<key column="CARD_NR" />
<many-to-many column="ID_PROMO" class="Promo"/>
</set>


The Promos.hbm.xml contains this mapping

<set name="cards" table="PROMO_CARD" cascade="save-update" lazy="true">
<key column="ID_PROMO"/>
<many-to-many column="CARD_NR" property-ref="cardSerialNr" class="Card"/>
</set>

Where am I wrong?
Should I add/remove another property to the set (ex: lazy="true")?
Thank you.


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Fri Apr 15, 2011 5:57 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
You also need to specify the property-ref for the key mapping on the Cards side


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Fri Apr 15, 2011 6:13 am 
Newbie

Joined: Wed Apr 13, 2011 8:58 am
Posts: 11
I did also this setting, but nothing was changed . It's still inserting the value of PK from CARD table. :(
It's strange and I really can't image why is not working well.

The Cards.hbm.xml

<property name="cardSerialNr" unique="true" column="CARD_NR"/>
<set name="promos" table="PROMO_CARD" cascade="save-update" lazy="true">
<key column="CARD_NR" property-ref="cardSerialNr"/>
<many-to-many column="ID_PROMO" class="Promo"/>
</set>


The Promos.hbm.xml

<set name="cards" table="PROMO_CARD" cascade="save-update" lazy="true">
<key column="ID_PROMO"/>
<many-to-many column="CARD_NR" property-ref="cardSerialNr" class="Card"/>
</set>


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Fri Apr 15, 2011 8:53 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
I tested it on my side and I got it working by specifying inverse="true" on one of the 2 set mapping.
It seems that you need to explicitly define which side of the relation is the owner ...


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Fri Apr 15, 2011 10:18 am 
Newbie

Joined: Wed Apr 13, 2011 8:58 am
Posts: 11
I putted inverse="true" in the Cards.hbm.xml, because I add or update cards by promoId and ... has the same result!

promo.getCards().add(card);

<set name="promos" table="PROMO_CARD" cascade="save-update" lazy="true" inverse="true">
<key column="CARD_NR" property-ref="cardSerialNr"/>
<many-to-many column="ID_PROMO" class="Promo"/>
</set>

If I set inverse="true" in the Promos.hbm.xml set, nothing is inserted.
:(
Thank you for help.


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Fri Apr 15, 2011 11:03 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Here is an example that works for me (Hibernate 3.2.6 with HSQLDB)

Code:
    <class name="Parent" table="PARENT">
        <id name="id" column="ID" type="long">
            <generator class="native"/>
        </id>
        <set name="children" table="PARENT_CHILD" inverse="false" cascade="all">
            <key column="parent_id"/>
            <many-to-many column="child_id" class="Child" property-ref="name"/>
        </set>
    </class>

    <class name="Child" table="CHILD">
        <id name="id" column="ID" type="long">
            <generator class="native"/>
        </id>
        <property name="name" column="NAME" type="string" unique="true"/>
        <set name="parents" table="PARENT_CHILD" inverse="true">
            <key column="child_id" property-ref="name"/>
            <many-to-many column="parent_id" class="Parent"/>
        </set>
    </class>


When I create a parent with 2 children and save the parent ... everything is fine!


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Fri Apr 15, 2011 11:38 am 
Newbie

Joined: Wed Apr 13, 2011 8:58 am
Posts: 11
And column "child_id" is the name of the column from PARENT_CHILD table, no?
I did exactly as you wrote above, but nothing is changed when create a parent with 2 children (in the table PROMO_CARD inserted the pair (id_promo, id_card).
Anyway, thank you again and have a nice weekend.


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Fri Apr 15, 2011 12:42 pm 
Newbie

Joined: Wed Apr 13, 2011 8:58 am
Posts: 11
I forgot to say that I use hibernate 2. Maybe this is the reason!


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Fri Apr 15, 2011 12:49 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
http://opensource.atlassian.com/project ... se/HB-1526

But according to this you should get a NPE ...


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Tue May 03, 2011 10:30 am 
Beginner
Beginner

Joined: Mon Apr 18, 2011 10:01 am
Posts: 20
Dear users, I've had the same exception while trying to map a <many-to-many> association, precisely as follows: I have created two tables in my Oracle 10G database :

CREATE TABLE P15_STEP_APPROVAZIONE
(
P15_TIPO_SEQUENZA VARCHAR2(16 BYTE) NOT NULL,
P15_ORDINALE NUMBER(4) NOT NULL,
P15_GROUPID_APPROVAZIONE VARCHAR2(32 BYTE) NOT NULL
);ALTER TABLE P15_STEP_APPROVAZIONE ADD (
CONSTRAINT P15_PK
PRIMARY KEY
(P15_TIPO_SEQUENZA, P15_ORDINALE);

CREATE TABLE PC_APPARTENENZA_RUOLI
(
ROLE_NAME VARCHAR2(50 CHAR) NOT NULL,
USER_NAME VARCHAR2(50 CHAR) NOT NULL
);ALTER TABLE PC_APPARTENENZA_RUOLI ADD (
CONSTRAINT PC_APPARTENENZA_RUOLI_PK
PRIMARY KEY
(ROLE_NAME, USER_NAME);

I want the two tables to reference each other with the fields ROLE_NAME=P15_GROUPID_APPROVAZIONE.

I have created the two javaBeans as follows :

Code:
package previtc.model.utente;
    public class AppartenenzaRuolo extends PrevItcDao implements Serializable {
    public AppartenenzaRuolo() {
    }
    private String user_name;
    private String role_name;
    private Set sequenzeApprovazione;
   
    private List listaRuoliAppartenenza;
   
   public Set getSequenzeApprovazione()
   {
      return this.sequenzeApprovazione;   
   }
   
   public void setSequenzeApprovazione(Set sequenzeApprovazione)
   {
       this.sequenzeApprovazione = sequenzeApprovazione;
   }
    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }
    public String getUser_name() {
        return user_name;
    }
    public void setRole_name(String role_name) {
        this.role_name = role_name;
    }
    public String getRole_name() {
        return role_name;
    }
    public void setListaRuoliAppartenenza(List listaRuoliAppartenenza) {
        this.listaRuoliAppartenenza = listaRuoliAppartenenza;
    }
    public List getListaRuoliAppartenenza() {
        return listaRuoliAppartenenza;
    }
}


and SequenzaApprovazioneStep.java

Code:
package previtc.model.approvazione;
public class SequenzaApprovazioneStep extends EntityObject implements Serializable {

    private String tipoSequenza;
    private Integer ordinale;
    private String groupIdApprovazione;

   private Set mansioni;
       
   public Set getMansioni()
   {
      return this.mansioni;   
   }
   
   public void setMansioni(Set mansioni)
   {
       this.mansioni = mansioni;
   }
   
    public SequenzaApprovazioneStep() {
    }

    public void setTipoSequenza(String tipoSequenza) {
        this.tipoSequenza = tipoSequenza;
    }

    public String getTipoSequenza() {
        return tipoSequenza;
    }

    public void setOrdinale(Integer ordinale) {
        this.ordinale = ordinale;
    }

    public Integer getOrdinale() {
        return ordinale;
    }

    public void setGroupIdApprovazione(String groupId) {
        this.groupIdApprovazione = groupId;
    }

    public String getGroupIdApprovazione() {
        return groupIdApprovazione;
    }


and my xml mapping files are :

Code:
<?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 package="previtc.model.utente">
    <class name="AppartenenzaRuolo" table="PC_APPARTENENZA_RUOLI">
        <composite-id>
            <key-property name="user_name" column="USER_NAME" type="string"/>
            <key-property name="role_name" column="ROLE_NAME" type="string"/>
        </composite-id>
        <set name="sequenze" table="P15_STEP_APPROVAZIONE">
            <key>
                <column name="P15_TIPO_SEQUENZA"/>
                <column name="P15_ORDINALE"/>
            </key>
           <many-to-many column="P15_GROUPID_APPROVAZIONE" class="previtc.model.approvazione.SequenzaApprovazioneStep" property-ref="ROLE_NAME"/>
       </set>
    </class>
</hibernate-mapping>


and for SequenzaApprovazioneStep.java
Code:
<?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 package="previtc.model.approvazione">
    <class name="SequenzaApprovazioneStep" table="P15_STEP_APPROVAZIONE"
            dynamic-update="true" dynamic-insert="true">
        <composite-id>
            <key-property name="tipoSequenza" column="P15_TIPO_SEQUENZA" type="string" />
            <key-property name="ordinale" column="P15_ORDINALE" type="integer" />
        </composite-id>
        <set name="mansioni" table="PC_APPARTENENZA_RUOLI" inverse="true">
           <key>
                <column name="USER_NAME"/>
                <column name="ROLE_NAME"/>
            </key>
           <many-to-many column="ROLE_NAME" class="previtc.model.utente.AppartenenzaRuolo" property-ref="P15_GROUPID_APPROVAZIONE"/>
       </set>
        <property name="groupIdApprovazione" column="P15_GROUPID_APPROVAZIONE" type="string"/>
    </class>
</hibernate-mapping>


But when I deploy the app in my OC4J server I get the same exception you mentioned above :
[...]
11/05/03 16:11:05 InitServlet - org.hibernate.MappingException: Foreign key (FKF971D6515A445E5:PC_APPARTENENZA_RUOLI [USER_NAME,ROLE_NAME])) must have same number of columns as the referenced primary
key (P15_STEP_APPROVAZIONE [P15_TIPO_SEQUENZA,P15_ORDINALE,P15_GROUPID_APPROVAZIONE])

at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
[...]

Could you give me any hints on how to solve this ?

Thanks a lot !


Top
 Profile  
 
 Post subject: Re: many-to-many - not by PK column
PostPosted: Tue May 03, 2011 11:25 am 
Newbie

Joined: Wed Apr 13, 2011 8:58 am
Posts: 11
Hello,

This error doesn't match exactly with the error that I had.
Property-ref is the property of class, not the column name of table.
Also, I don't think you can have only one FK column, as long as the referenced table has composite primary key!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.