【发布时间】:2015-08-07 11:29:42
【问题描述】:
我正在使用 Spring MVC 和休眠进行 Spring 项目
我有两个班级 Employe 和 Department。在数据库中,表 EMPLOYE 我有一个 EDEPARTEMENT,它是一个引用 DEPARTEMENT(ID)
我正在处理这个案例(UML):imgur
我正在使用 hbm.xml 文件进行休眠映射
这是我的 Departement.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">
<!-- Generated 5 ao?t 2015 11:05:44 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.my.dao.Departement" table="DEPARTEMENT">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="nomDepartement" type="java.lang.String">
<column name="NOMDEPARTEMENT" />
</property>
<set name="Employe" inverse="true">
<key column="ID" not-null="true"/>
<one-to-many class="com.my.dao.Employe" />
</set>
</class>
</hibernate-mapping>
这是我的 Employe.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">
<!-- Generated 5 ao?t 2015 11:05:44 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.my.dao.Employe" table="EMPLOYE">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="nomEmploye" type="java.lang.String">
<column name="NOMEMPLOYE" />
</property>
<property name="prenomEmploye" type="java.lang.String">
<column name="PRENOMEMPLOYE" />
</property>
<property name="matriculeMY" type="int">
<column name="MATRICULEMY" />
</property>
<property name="adresse" type="java.lang.String">
<column name="ADRESSE" />
</property>
<property name="sexe" type="java.lang.String">
<column name="SEXE" />
</property>
<property name="cin" type="java.lang.String">
<column name="CIN" />
</property>
<property name="dateNaissance" type="java.lang.String">
<column name="DATENAISSANCE" />
</property>
<property name="situationFamiliale" type="java.lang.String">
<column name="SITUATIONFAMILIALE" />
</property>
<property name="dateEntree" type="java.lang.String">
<column name="DATEENTREE" />
</property>
<property name="dateSortie" type="java.lang.String">
<column name="DATESORTIE" />
</property>
<property name="numCIMR" type="java.lang.String">
<column name="NUMCIMR" />
</property>
<property name="numCNSS" type="java.lang.String">
<column name="NUMCNSS" />
</property>
<property name="numMUT" type="java.lang.String">
<column name="NUMMUT" />
</property>
<property name="profile" type="java.lang.String">
<column name="PROFILE" />
</property>
<property name="resteConge" type="java.lang.String">
<column name="RESTECONGE" />
</property>
<property name="banque" type="java.lang.String">
<column name="BANQUE" />
</property>
<property name="numCpteBanc" type="java.lang.String">
<column name="NUMCPTEBANC" />
</property>
<!-- <property name="fonction" type="java.lang.String">
<column name="FONCTION" />
</property> -->
<property name="salaire" type="float">
<column name="SALAIRE" />
</property>
<property name="indTransport" type="float">
<column name="INDTRANSPORT" />
</property>
<property name="indRepresent" type="float">
<column name="INDREPRESENT" />
</property>
<property name="indPanier" type="float">
<column name="INDPANIER" />
</property>
<many-to-one name="eDepartement" class="com.my.dao.Departement" access="field" fetch="join">
<column name="EDEPARTEMENT" />
</many-to-one>
<many-to-one name="eFonction" class="com.my.dao.Fonction" access="field" fetch="join">
<column name="EFONCTION" />
</many-to-one>
<many-to-one name="eService" class="com.my.dao.Service" access="field" fetch="join">
<column name="ESERVICE" />
</many-to-one>
<many-to-one name="eTypePaiement" class="com.my.dao.TypePaiement" access="field" fetch="join">
<column name="ETYPEPAIEMENT" />
</many-to-one>
<many-to-one name="eModePaiement" class="com.my.dao.ModePaiement" access="field" fetch="join">
<column name="EMODEPAIEMENT" />
</many-to-one>
</class>
</hibernate-mapping>
这是我的 com.my.dao.Employe.java 类
package com.my.dao;
import java.util.Date;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import org.hibernate.annotations.ManyToAny;
/**
* @author Ilias
* @version 1.0
*/
public class Employe {
private int id;
private String nomEmploye;
private String prenomEmploye;
private int matriculeMY;
private String adresse;
private String sexe;
private String cin;
private Date dateNaissance;
private String situationFamiliale;
private int nombreEnfant;
private Date dateEntree;
private Date dateSortie;
private String numCIMR;
private String numCNSS;
private String numMUT;
private String profile;
private String resteConge;
private String banque;
private String numCpteBanc;
private String fonction;
private float salaire;
private float indTransport;
private float indRepresent;
private float indPanier;
private Departement eDepartement;
private Fonction eFonction;
private Service eService;
private TypePaiement eTypePaiement;
private ModePaiement eModePaiement;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the nomEmploye
*/
public String getNomEmploye() {
return nomEmploye;
}
/**
* @param nomEmploye the nomEmploye to set
*/
public void setNomEmploye(String nomEmploye) {
this.nomEmploye = nomEmploye;
}
/**
* @return the prenomEmploye
*/
public String getPrenomEmploye() {
return prenomEmploye;
}
/**
* @param prenomEmploye the prenomEmploye to set
*/
public void setPrenomEmploye(String prenomEmploye) {
this.prenomEmploye = prenomEmploye;
}
/**
* @return the matriculeMY
*/
public int getMatriculeMY() {
return matriculeMY;
}
/**
* @param matriculeMY the matriculeMY to set
*/
public void setMatriculeMY(int matriculeMY) {
this.matriculeMY = matriculeMY;
}
/**
* @return the adresse
*/
public String getAdresse() {
return adresse;
}
/**
* @param adresse the adresse to set
*/
public void setAdresse(String adresse) {
this.adresse = adresse;
}
/**
* @return the sexe
*/
public String getSexe() {
return sexe;
}
/**
* @param sexe the sexe to set
*/
public void setSexe(String sexe) {
this.sexe = sexe;
}
/**
* @return the cin
*/
public String getCin() {
return cin;
}
/**
* @param cin the cin to set
*/
public void setCin(String cin) {
this.cin = cin;
}
/**
* @return the dateNaissance
*/
public Date getDateNaissance() {
return dateNaissance;
}
/**
* @param dateNaissance the dateNaissance to set
*/
public void setDateNaissance(Date dateNaissance) {
this.dateNaissance = dateNaissance;
}
/**
* @return the situationFamiliale
*/
public String getSituationFamiliale() {
return situationFamiliale;
}
/**
* @param situationFamiliale the situationFamiliale to set
*/
public void setSituationFamiliale(String situationFamiliale) {
this.situationFamiliale = situationFamiliale;
}
/**
* @return the nombreEnfant
*/
public int getNombreEnfant() {
return nombreEnfant;
}
/**
* @param nombreEnfant the nombreEnfant to set
*/
public void setNombreEnfant(int nombreEnfant) {
this.nombreEnfant = nombreEnfant;
}
/**
* @return the dateEntree
*/
public Date getDateEntree() {
return dateEntree;
}
/**
* @param dateEntree the dateEntree to set
*/
public void setDateEntree(Date dateEntree) {
this.dateEntree = dateEntree;
}
/**
* @return the dateSortie
*/
public Date getDateSortie() {
return dateSortie;
}
/**
* @param dateSortie the dateSortie to set
*/
public void setDateSortie(Date dateSortie) {
this.dateSortie = dateSortie;
}
/**
* @return the numCIMR
*/
public String getNumCIMR() {
return numCIMR;
}
/**
* @param numCIMR the numCIMR to set
*/
public void setNumCIMR(String numCIMR) {
this.numCIMR = numCIMR;
}
/**
* @return the numCNSS
*/
public String getNumCNSS() {
return numCNSS;
}
/**
* @param numCNSS the numCNSS to set
*/
public void setNumCNSS(String numCNSS) {
this.numCNSS = numCNSS;
}
/**
* @return the numMUT
*/
public String getNumMUT() {
return numMUT;
}
/**
* @param numMUT the numMUT to set
*/
public void setNumMUT(String numMUT) {
this.numMUT = numMUT;
}
/**
* @return the profile
*/
public String getProfile() {
return profile;
}
/**
* @param profile the profile to set
*/
public void setProfile(String profile) {
this.profile = profile;
}
/**
* @return the resteConge
*/
public String getResteConge() {
return resteConge;
}
/**
* @param resteConge the resteConge to set
*/
public void setResteConge(String resteConge) {
this.resteConge = resteConge;
}
/**
* @return the banque
*/
public String getBanque() {
return banque;
}
/**
* @param banque the banque to set
*/
public void setBanque(String banque) {
this.banque = banque;
}
/**
* @return the numCpteBanc
*/
public String getNumCpteBanc() {
return numCpteBanc;
}
/**
* @param numCpteBanc the numCpteBanc to set
*/
public void setNumCpteBanc(String numCpteBanc) {
this.numCpteBanc = numCpteBanc;
}
/**
* @return the fonction
*/
public String getFonction() {
return fonction;
}
/**
* @param fonction the fonction to set
*/
public void setFonction(String fonction) {
this.fonction = fonction;
}
/**
* @return the salaire
*/
public float getSalaire() {
return salaire;
}
/**
* @param salaire the salaire to set
*/
public void setSalaire(float salaire) {
this.salaire = salaire;
}
/**
* @return the indTransport
*/
public float getIndTransport() {
return indTransport;
}
/**
* @param indTransport the indTransport to set
*/
public void setIndTransport(float indTransport) {
this.indTransport = indTransport;
}
/**
* @return the indRepresent
*/
public float getIndRepresent() {
return indRepresent;
}
/**
* @param indRepresent the indRepresent to set
*/
public void setIndRepresent(float indRepresent) {
this.indRepresent = indRepresent;
}
/**
* @return the indPanier
*/
public float getIndPanier() {
return indPanier;
}
/**
* @param indPanier the indPanier to set
*/
public void setIndPanier(float indPanier) {
this.indPanier = indPanier;
}
/**
* @return the eDepartement
*/
public Departement geteDepartement() {
return eDepartement;
}
/**
* @param eDepartement the eDepartement to set
*/
public void seteDepartement(Departement eDepartement) {
this.eDepartement = eDepartement;
}
/**
* @return the eFonction
*/
public Fonction geteFonction() {
return eFonction;
}
/**
* @param eFonction the eFonction to set
*/
public void seteFonction(Fonction eFonction) {
this.eFonction = eFonction;
}
/**
* @return the eService
*/
public Service geteService() {
return eService;
}
/**
* @param eService the eService to set
*/
public void seteService(Service eService) {
this.eService = eService;
}
/**
* @return the eTypePaiement
*/
public TypePaiement geteTypePaiement() {
return eTypePaiement;
}
/**
* @param eTypePaiement the eTypePaiement to set
*/
public void seteTypePaiement(TypePaiement eTypePaiement) {
this.eTypePaiement = eTypePaiement;
}
/**
* @return the eModePaiement
*/
public ModePaiement geteModePaiement() {
return eModePaiement;
}
/**
* @param eModePaiement the eModePaiement to set
*/
public void seteModePaiement(ModePaiement eModePaiement) {
this.eModePaiement = eModePaiement;
}
/**
* @param nomEmploye
* @param prenomEmploye
* @param matriculeMY
* @param adresse
* @param sexe
* @param cin
* @param dateNaissance
* @param situationFamiliale
* @param nombreEnfant
* @param dateEntree
* @param dateSortie
* @param numCIMR
* @param numCNSS
* @param numMUT
* @param profile
* @param resteConge
* @param banque
* @param numCpteBanc
* @param fonction
* @param salaire
* @param indTransport
* @param indRepresent
* @param indPanier
* @param eDepartement
* @param eFonction
* @param eService
* @param eTypePaiement
* @param eModePaiement
*/
public Employe(String nomEmploye, String prenomEmploye, int matriculeMY, String adresse, String sexe, String cin,
Date dateNaissance, String situationFamiliale, int nombreEnfant, Date dateEntree, Date dateSortie,
String numCIMR, String numCNSS, String numMUT, String profile, String resteConge, String banque,
String numCpteBanc, String fonction, float salaire, float indTransport, float indRepresent, float indPanier,
Departement eDepartement, Fonction eFonction, Service eService, TypePaiement eTypePaiement,
ModePaiement eModePaiement) {
super();
this.nomEmploye = nomEmploye;
this.prenomEmploye = prenomEmploye;
this.matriculeMY = matriculeMY;
this.adresse = adresse;
this.sexe = sexe;
this.cin = cin;
this.dateNaissance = dateNaissance;
this.situationFamiliale = situationFamiliale;
this.nombreEnfant = nombreEnfant;
this.dateEntree = dateEntree;
this.dateSortie = dateSortie;
this.numCIMR = numCIMR;
this.numCNSS = numCNSS;
this.numMUT = numMUT;
this.profile = profile;
this.resteConge = resteConge;
this.banque = banque;
this.numCpteBanc = numCpteBanc;
this.fonction = fonction;
this.salaire = salaire;
this.indTransport = indTransport;
this.indRepresent = indRepresent;
this.indPanier = indPanier;
this.eDepartement = eDepartement;
this.eFonction = eFonction;
this.eService = eService;
this.eTypePaiement = eTypePaiement;
this.eModePaiement = eModePaiement;
}
public Employe(Employe e){
this.setNomEmploye(e.nomEmploye);
this.setPrenomEmploye(e.prenomEmploye);
this.setCin(e.cin);
this.setDateNaissance(e.dateNaissance);
this.setSituationFamiliale(e.situationFamiliale);
this.setNombreEnfant(e.nombreEnfant);
this.setDateEntree(e.dateEntree);
this.setDateSortie(e.dateSortie);
this.setNumCIMR(e.numCIMR);
this.setNumCNSS(e.numCNSS);
this.setNumMUT(e.numMUT);
this.setProfile(e.profile);
this.setResteConge(e.resteConge);
this.setBanque(e.banque);
this.setNumCpteBanc(e.numCpteBanc);
this.setSalaire(e.salaire);
this.setIndTransport(e.indTransport);
this.setIndRepresent(e.indRepresent);
this.setIndPanier(e.indPanier);
//this.setFonction(e.fonction);
this.seteDepartement(e.eDepartement);
this.seteFonction(e.eFonction);
this.seteService(e.eService);
this.seteModePaiement(e.eModePaiement);
this.seteTypePaiement(e.eTypePaiement);
}
public Employe() {
super();
// TODO Auto-generated constructor stub
}
/**
* {@exception} Throwable
*/
public void finalize() throws Throwable{
}
}
问题是:当我运行项目并将 Employe 添加到 DB 时,Departement 将插入到 DB 但EDEPARTEMENT(外键)将为空。
我该如何处理,以及如何避免部门表中的重复条目?
【问题讨论】:
-
请出示您插入
Employe对象的代码。 -
@Jens 我更新问题
-
看不到任何您尝试插入对象的代码。不要混合使用注解和基于 xml 的配置。
-
@Jens 不不,这只是一个旧代码,我会更新代码
-
@Jens 我修改了代码
标签: java mysql spring hibernate hibernate-mapping