我试图弄清楚如何使用JPA注释和Hibernate配置多对多关系,我阅读了很多文章和论坛讨论,但我最终得到了一个错误。
我想知道的第一件事是: 配置与属性的多对多关系
我有两个表设备和维护公司以及关系赔偿
当我为关系添加一个实体并在其他关系中创建一些@OneToMany时,我遇到了组合主键的问题,所以我使用了Collection方法,但我有这个例外:
org.hibernate.MappingException: Repeated column in mapping for collection: mmapp.domain.Equipement.reparations column: EQUIPEMENT_FK
设备:
@Entity
@Table( name = "EQUIPEMENTS" )
public class Equipement implements Serializable{
private int id ;
private String marque ;
private int isbn ;
private Date purchaseDate ;
private double price ;
private int warrantyPeriod ;
public Equipement(){
}
public Equipement( String marque ) {
this.marque = marque ;
}
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "EQUIPEMENT_ID" )
public int getId(){
return this.id ;
}
public void setId(int id){
this.id = id ;
}
@Column( name = "EQUIPEMENT_MARQUE" )
public String getMarque(){
return this.marque ;
}
public void setMarque(String marque){
this.marque = marque ;
}
@Column( name = "EQUIPEMENT_PUR_DATE" )
public Date getPurchaseDate(){
return this.purchaseDate ;
}
public void setPurchaseDate(Date purchaseDate){
this.purchaseDate = purchaseDate ;
}
@Column( name = "EQUIPEMENT_PRICE" )
public double getPrice(){
return this.price ;
}
public void setPrice(double price){
this.price = price ;
}
@ElementCollection
@JoinTable(
name= "EQUIPEMENT_MAINTENANCE_COMPANY" ,
joinColumns=@JoinColumn( name= "EQUIPEMENT_FK" ))
public Collection<Reparation> getReparations() {
return reparations ;
}
public void setReparations(Collection<Reparation> reparations) {
this.reparations = reparations ;
}
private Collection<Reparation> reparations ;
}
维护公司:
@Entity
@Table( name = "MAINTENANCE_COMPANIES" )
public class MaintenanceCompany implements Serializable{
private int id ;
private String name ;
private String adress ;
private String telephone ;
public MaintenanceCompany(){}
public MaintenanceCompany( String name ) {
this.name = name ;
}
public void setId(int id){
this.id = id ;
}
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
public int getId(){
return this.id ;
}
public void setName(String name){
this.name = name ;
}
public String getName(){
return this.name ;
}
public void setAdress(String adress){
this.adress = adress ;
}
public String getAdress(){
return this.adress ;
}
public void setTelephone(String telephone){
this.telephone = telephone ;
}
public String getTelephone(){
return this.telephone ;
}
@ElementCollection
@JoinTable(
name= "EQUIPEMENT_MAINTENANCE_COMPANY" ,
joinColumns=@JoinColumn( name= "COMPANY_FK" ))
public Collection<Reparation> getReparations(){
return reparations ;
}
public void setReparations(Collection<Reparation> reparations){
this.reparations = reparations ;
}
private Collection<Reparation> reparations ;
}
赔偿
@Embeddable
public class Reparation implements Serializable{
private int delay ;
private double price ;
private Date date ;
public Reparation() {}
public Reparation(int delay, double price, Date date) {
this.delay = delay ;
this.price = price ;
this.date = date ;
}
public int getDelay() {
return delay ;
}
public void setDelay( int delay ) {
this.delay = delay ;
}
public double getPrice() {
return price ;
}
public void setPrice( double price ) {
this.price = price ;
}
public Date getDate() {
return date ;
}
public void setDate( Date date ) {
this.date = date ;
}
@ManyToOne( fetch = FetchType.LAZY )
@JoinColumn( name = "EQUIPEMENT_FK" )
public Equipement getEquipementRepared(){
return equipementRepared ;
}
public void setEquipementRepared(Equipement equipementRepared) {
this.equipementRepared = equipementRepared ;
}
private Equipement equipementRepared ;
@ManyToOne( fetch = FetchType.LAZY )
@JoinColumn( name = "COMPANY_FK" )
public MaintenanceCompany getMaintenanceCompany() {
return maintenanceCompany ;
}
public void setMaintenanceCompany( MaintenanceCompany maintenanceCompany ) {
this.maintenanceCompany = maintenanceCompany ;
}
private MaintenanceCompany maintenanceCompany ;
}
答案 0 :(得分:3)
这种映射没有多大意义,并且与您首先想要的内容相差甚远:与属性的多对多关系。
多对多使用两个实体之间的连接表。如果此连接表中有两个以上的ID,则它不再是连接表,而是一个功能实体,应该映射为任何其他实体,并具有自己自动生成的ID。
因此,您应该有3个实体,每个实体都有自己生成的ID:
One Equipment有很多赔偿。一个MaintenanceCompany有很多赔偿。 One Reparation有一台设备和一台MaintenanceCompany。
@Entity
public class Equipment
@OneToMany(mappedBy = "equipment")
private Set<Reparation> reparations;
// ID, fields, methods
}
@Entity
public class MaintenanceCompany
@OneToMany(mappedBy = "maintenanceCompany")
private Set<Reparation> reparations;
// ID, fields, methods
}
@Entity
public class Reparation
@ManyToOne
@JoinColumn(name = "equipment_id")
private Equipment equipment;
@ManyToOne
@JoinColumn(name = "maintenance_company_id")
private MaintenanceCompany maintenanceCompany;
// ID, fields, methods
}
就这么简单。