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 !