-->
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.  [ 4 posts ] 
Author Message
 Post subject: Help on mapping composite key
PostPosted: Tue May 03, 2011 7:37 am 
Newbie

Joined: Tue May 03, 2011 7:32 am
Posts: 3
Hello, i am facing a problem that i can't resolve i looked everywhere on forums but unable to get over it. i use JSF 2.0 for the views, and hibernate for persistance. here is my problem now:
i have 2 classes Ordre_miss(num_orm,Organisme_code_org) i indicated only the Primary key, the other class is Mission(Ordre_miss_num_orm,Compagnie_aer_code_cmp_aer)
Also MissionPK handles the primary key of Table Mission and OrdremissPK hadles primary key of table Ordre_miss, the primary key have been put in separate classes by NetBeans 6.9.1 i used it's entiyti generator and the mapping generator.

I get the following error
GRAVE: Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FKA2F7196CED5FD03A:Mission [Ordre_miss_num_orm])) must have same number of columns as the referenced primary key (Ordre_miss [num_orm,Agent_mat_agt])

i can't figure out how to correctly map the relation.

Class Mission

Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package gest;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
*
* @author Moh
*/
@Entity
@Table(name = "Mission")
@NamedQueries({
    @NamedQuery(name = "Mission.findAll", query = "SELECT m FROM Mission m"),
    @NamedQuery(name = "Mission.findByOrdremissnumorm", query = "SELECT m FROM Mission m WHERE m.missionPK.ordremissnumorm = :ordremissnumorm"),
    @NamedQuery(name = "Mission.findByCompagnieaercodecmpaer", query = "SELECT m FROM Mission m WHERE m.missionPK.compagnieaercodecmpaer = :compagnieaercodecmpaer"),
    @NamedQuery(name = "Mission.findByNbrJr100", query = "SELECT m FROM Mission m WHERE m.nbrJr100 = :nbrJr100"),
    @NamedQuery(name = "Mission.findByNbrJr75", query = "SELECT m FROM Mission m WHERE m.nbrJr75 = :nbrJr75"),
    @NamedQuery(name = "Mission.findByDateDepartM", query = "SELECT m FROM Mission m WHERE m.dateDepartM = :dateDepartM"),
    @NamedQuery(name = "Mission.findByDateRetM", query = "SELECT m FROM Mission m WHERE m.dateRetM = :dateRetM")})
public class Mission implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected MissionPK missionPK;
    @Basic(optional = false)
    @Column(name = "nbr_jr_100")
    private int nbrJr100;
    @Basic(optional = false)
    @Column(name = "nbr_jr_75")
    private int nbrJr75;
    @Basic(optional = false)
    @Column(name = "date_depart_m")
    private int dateDepartM;
    @Basic(optional = false)
    @Column(name = "date_ret_m")
    private int dateRetM;
    @JoinColumn(name = "Ordre_miss_num_orm", referencedColumnName = "num_orm", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Ordremiss ordremiss;
    @JoinColumn(name = "Compagnie_aer_code_cmp_aer", referencedColumnName = "code_cmp_aer", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Compagnieaer compagnieaer;

    public Mission() {
    }

    public Mission(MissionPK missionPK) {
        this.missionPK = missionPK;
    }

    public Mission(MissionPK missionPK, int nbrJr100, int nbrJr75, int dateDepartM, int dateRetM) {
        this.missionPK = missionPK;
        this.nbrJr100 = nbrJr100;
        this.nbrJr75 = nbrJr75;
        this.dateDepartM = dateDepartM;
        this.dateRetM = dateRetM;
    }

    public Mission(String ordremissnumorm, int compagnieaercodecmpaer) {
        this.missionPK = new MissionPK(ordremissnumorm, compagnieaercodecmpaer);
    }

    public MissionPK getMissionPK() {
        return missionPK;
    }

    public void setMissionPK(MissionPK missionPK) {
        this.missionPK = missionPK;
    }

    public int getNbrJr100() {
        return nbrJr100;
    }

    public void setNbrJr100(int nbrJr100) {
        this.nbrJr100 = nbrJr100;
    }

    public int getNbrJr75() {
        return nbrJr75;
    }

    public void setNbrJr75(int nbrJr75) {
        this.nbrJr75 = nbrJr75;
    }

    public int getDateDepartM() {
        return dateDepartM;
    }

    public void setDateDepartM(int dateDepartM) {
        this.dateDepartM = dateDepartM;
    }

    public int getDateRetM() {
        return dateRetM;
    }

    public void setDateRetM(int dateRetM) {
        this.dateRetM = dateRetM;
    }

    public Ordremiss getOrdremiss() {
        return ordremiss;
    }

    public void setOrdremiss(Ordremiss ordremiss) {
        this.ordremiss = ordremiss;
    }

    public Compagnieaer getCompagnieaer() {
        return compagnieaer;
    }

    public void setCompagnieaer(Compagnieaer compagnieaer) {
        this.compagnieaer = compagnieaer;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (missionPK != null ? missionPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Mission)) {
            return false;
        }
        Mission other = (Mission) object;
        if ((this.missionPK == null && other.missionPK != null) || (this.missionPK != null && !this.missionPK.equals(other.missionPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "gest.Mission[missionPK=" + missionPK + "]";
    }

}


Class MissionPK

Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package gest;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
*
* @author Moh
*/
@Embeddable
public class MissionPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "Ordre_miss_num_orm")
    private String ordremissnumorm;
    @Basic(optional = false)
    @Column(name = "Compagnie_aer_code_cmp_aer")
    private int compagnieaercodecmpaer;

    public MissionPK() {
    }

    public MissionPK(String ordremissnumorm, int compagnieaercodecmpaer) {
        this.ordremissnumorm = ordremissnumorm;
        this.compagnieaercodecmpaer = compagnieaercodecmpaer;
    }

    public String getOrdremissnumorm() {
        return ordremissnumorm;
    }

    public void setOrdremissnumorm(String ordremissnumorm) {
        this.ordremissnumorm = ordremissnumorm;
    }

    public int getCompagnieaercodecmpaer() {
        return compagnieaercodecmpaer;
    }

    public void setCompagnieaercodecmpaer(int compagnieaercodecmpaer) {
        this.compagnieaercodecmpaer = compagnieaercodecmpaer;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (ordremissnumorm != null ? ordremissnumorm.hashCode() : 0);
        hash += (int) compagnieaercodecmpaer;
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof MissionPK)) {
            return false;
        }
        MissionPK other = (MissionPK) object;
        if ((this.ordremissnumorm == null && other.ordremissnumorm != null) || (this.ordremissnumorm != null && !this.ordremissnumorm.equals(other.ordremissnumorm))) {
            return false;
        }
        if (this.compagnieaercodecmpaer != other.compagnieaercodecmpaer) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "gest.MissionPK[ordremissnumorm=" + ordremissnumorm + ", compagnieaercodecmpaer=" + compagnieaercodecmpaer + "]";
    }

}


Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package gest;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
*
* @author Moh
*/
@Entity
@Table(name = "Ordre_miss")
@NamedQueries({
    @NamedQuery(name = "Ordremiss.findAll", query = "SELECT o FROM Ordremiss o"),
    @NamedQuery(name = "Ordremiss.findByAgentmatagt", query = "SELECT o FROM Ordremiss o WHERE o.ordremissPK.agentmatagt = :agentmatagt"),
    @NamedQuery(name = "Ordremiss.findByNumOrm", query = "SELECT o FROM Ordremiss o WHERE o.ordremissPK.numOrm = :numOrm"),
    @NamedQuery(name = "Ordremiss.findByDateDepOrm", query = "SELECT o FROM Ordremiss o WHERE o.dateDepOrm = :dateDepOrm"),
    @NamedQuery(name = "Ordremiss.findByDateRetOrm", query = "SELECT o FROM Ordremiss o WHERE o.dateRetOrm = :dateRetOrm"),
    @NamedQuery(name = "Ordremiss.findByNatureOrm", query = "SELECT o FROM Ordremiss o WHERE o.natureOrm = :natureOrm"),
    @NamedQuery(name = "Ordremiss.findByEtatOrm", query = "SELECT o FROM Ordremiss o WHERE o.etatOrm = :etatOrm")})
public class Ordremiss implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected OrdremissPK ordremissPK;
    @Basic(optional = false)
    @Column(name = "date_dep_orm")
    @Temporal(TemporalType.DATE)
    private Date dateDepOrm;
    @Basic(optional = false)
    @Column(name = "date_ret_orm")
    @Temporal(TemporalType.DATE)
    private Date dateRetOrm;
    @Basic(optional = false)
    @Column(name = "nature_orm")
    private String natureOrm;
    @Basic(optional = false)
    @Column(name = "etat_orm")
    private String etatOrm;
    @JoinColumn(name = "Organisme_code_org", referencedColumnName = "code_org")
    @ManyToOne
    private Organisme organisme;
    @JoinColumn(name = "Agent_mat_agt", referencedColumnName = "mat_agt", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Agent agent;

    public Ordremiss() {
    }

    public Ordremiss(OrdremissPK ordremissPK) {
        this.ordremissPK = ordremissPK;
    }

    public Ordremiss(OrdremissPK ordremissPK, Date dateDepOrm, Date dateRetOrm, String natureOrm, String etatOrm) {
        this.ordremissPK = ordremissPK;
        this.dateDepOrm = dateDepOrm;
        this.dateRetOrm = dateRetOrm;
        this.natureOrm = natureOrm;
        this.etatOrm = etatOrm;
    }

    public Ordremiss(String agentmatagt, String numOrm) {
        this.ordremissPK = new OrdremissPK(agentmatagt, numOrm);
    }

    public OrdremissPK getOrdremissPK() {
        return ordremissPK;
    }

    public void setOrdremissPK(OrdremissPK ordremissPK) {
        this.ordremissPK = ordremissPK;
    }

    public Date getDateDepOrm() {
        return dateDepOrm;
    }

    public void setDateDepOrm(Date dateDepOrm) {
        this.dateDepOrm = dateDepOrm;
    }

    public Date getDateRetOrm() {
        return dateRetOrm;
    }

    public void setDateRetOrm(Date dateRetOrm) {
        this.dateRetOrm = dateRetOrm;
    }

    public String getNatureOrm() {
        return natureOrm;
    }

    public void setNatureOrm(String natureOrm) {
        this.natureOrm = natureOrm;
    }

    public String getEtatOrm() {
        return etatOrm;
    }

    public void setEtatOrm(String etatOrm) {
        this.etatOrm = etatOrm;
    }

    public Organisme getOrganisme() {
        return organisme;
    }

    public void setOrganisme(Organisme organisme) {
        this.organisme = organisme;
    }

    public Agent getAgent() {
        return agent;
    }

    public void setAgent(Agent agent) {
        this.agent = agent;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (ordremissPK != null ? ordremissPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Ordremiss)) {
            return false;
        }
        Ordremiss other = (Ordremiss) object;
        if ((this.ordremissPK == null && other.ordremissPK != null) || (this.ordremissPK != null && !this.ordremissPK.equals(other.ordremissPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "gest.Ordremiss[ordremissPK=" + ordremissPK + "]";
    }

}


Class OrdremissPK

Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package gest;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
*
* @author Moh
*/
@Embeddable
public class OrdremissPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "Agent_mat_agt")
    private String agentmatagt;
    @Basic(optional = false)
    @Column(name = "num_orm")
    private String numOrm;

    public OrdremissPK() {
    }

    public OrdremissPK(String agentmatagt, String numOrm) {
        this.agentmatagt = agentmatagt;
        this.numOrm = numOrm;
    }

    public String getAgentmatagt() {
        return agentmatagt;
    }

    public void setAgentmatagt(String agentmatagt) {
        this.agentmatagt = agentmatagt;
    }

    public String getNumOrm() {
        return numOrm;
    }

    public void setNumOrm(String numOrm) {
        this.numOrm = numOrm;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (agentmatagt != null ? agentmatagt.hashCode() : 0);
        hash += (numOrm != null ? numOrm.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof OrdremissPK)) {
            return false;
        }
        OrdremissPK other = (OrdremissPK) object;
        if ((this.agentmatagt == null && other.agentmatagt != null) || (this.agentmatagt != null && !this.agentmatagt.equals(other.agentmatagt))) {
            return false;
        }
        if ((this.numOrm == null && other.numOrm != null) || (this.numOrm != null && !this.numOrm.equals(other.numOrm))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "gest.OrdremissPK[agentmatagt=" + agentmatagt + ", numOrm=" + numOrm + "]";
    }

}


here are the mappings:
Ordremiss.hbm.xml
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">
<!-- Generated 29 avr. 2011 11:38:07 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="gest.Ordremiss" table="Ordre_miss" catalog="gest_miss">
        <composite-id name="id" class="gest.OrdreMissPK">
            <key-property name="numOrm" type="string">
                <column name="num_orm" length="12" />
            </key-property>
            <key-property name="agentMatAgt" type="string">
                <column name="Agent_mat_agt" length="10" />
            </key-property>
        </composite-id>
        <many-to-one name="organisme" class="gest.Organisme" fetch="select">
            <column name="Organisme_code_org" />
        </many-to-one>
        <many-to-one name="agent" class="gest.Agent" update="false" insert="false" fetch="select">
            <column name="Agent_mat_agt" length="10" not-null="true" />
        </many-to-one>
        <property name="dateDepOrm" type="date">
            <column name="date_dep_orm" length="10" not-null="true" />
        </property>
        <property name="dateRetOrm" type="date">
            <column name="date_ret_orm" length="10" not-null="true" />
        </property>
        <property name="natureOrm" type="string">
            <column name="nature_orm" length="45" not-null="true" />
        </property>
        <property name="etatOrm" type="string">
            <column name="etat_orm" length="45" not-null="true" />
        </property>
        <set name="missions" inverse="true">
            <key>
                <column name="Ordre_miss_num_orm" length="12" not-null="true" />
            </key>
            <one-to-many class="gest.Mission" />
        </set>
    </class>
</hibernate-mapping>


Mission.hbm.xml

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">
<!-- Generated 30 mars 2011 09:49:33 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="gest.Mission" table="Mission" catalog="gest_miss">
        <composite-id name="missionPK" class="gest.MissionPK">
            <key-property name="ordremissnumorm" type="string">
                <column name="Ordre_miss_num_orm" length="12" />
            </key-property>
            <key-property name="compagnieaercodecmpaer" type="int">
                <column name="Compagnie_aer_code_cmp_aer" />
            </key-property>           
        </composite-id>
        <many-to-one name="ordremiss" class="gest.Ordremiss" update="false" insert="false" fetch="select">
            <column name="num_orm" length="12" not-null="true" />
        </many-to-one>
        <many-to-one name="compagnieaer" class="gest.Compagnieaer" update="false" insert="false" fetch="select">
            <column name="Compagnie_aer_code_cmp_aer" not-null="true" />
        </many-to-one>
        <property name="nbrJr100" type="int">
            <column name="nbr_jr_100" not-null="true" />
        </property>
        <property name="nbrJr75" type="int">
            <column name="nbr_jr_75" not-null="true" />
        </property>
        <property name="dateDepartM" type="int">
            <column name="date_depart_m" not-null="true" />
        </property>
        <property name="dateRetM" type="int">
            <column name="date_ret_m" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


Those mappings have not been modified by me except some variable names.
I really need help on this, thanks in advance.


Top
 Profile  
 
 Post subject: Re: Help on mapping composite key
PostPosted: Wed May 04, 2011 5:18 am 
Newbie

Joined: Tue May 03, 2011 7:32 am
Posts: 3
Anyone knows how to help me please?


Top
 Profile  
 
 Post subject: Re: Help on mapping composite key
PostPosted: Sat May 07, 2011 6:26 am 
Newbie

Joined: Tue May 03, 2011 7:32 am
Posts: 3
Please can someone take a look and give me a hand?


Top
 Profile  
 
 Post subject: Re: Help on mapping composite key
PostPosted: Wed May 18, 2011 1:48 am 
Newbie

Joined: Wed May 18, 2011 1:25 am
Posts: 1
You need to correct the mapping entry in Mission.hbm.xml

<many-to-one name="ordremiss" class="gest.Ordremiss" update="false" insert="false" fetch="select">
<column name="num_orm" length="12" not-null="true" />
<column name="Agent_mat_agt" length="10" />
</many-to-one>


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