1

我试图弄清楚如何使用 JPA 注释和 Hibernate 配置多对多关系,我阅读了许多文章和论坛讨论,但最终出现错误。

我想知道的第一件事是如何: 配置与属性的多对多关系

我有两个表EquipementsMaintenance_Companies以及关系Reparation

当我为关系添加一个实体并在其他实体中创建一些 @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 ;
}
4

1 回答 1

3

这种映射没有多大意义,并且与您最初所说的想要的相去甚远:与属性的多对多关系。

多对多在两个实体之间使用连接表。如果这个连接表中有两个以上的ID,那么它就不再是一个连接表,而是一个功能实体,它应该被映射为任何其他实体,并且有自己的自动生成的ID。

因此,您应该有 3 个实体,每个实体都有自己生成的 ID:

  • 设备
  • 赔偿
  • 维修公司

一台设备有许多修理。一家MaintenanceCompany 有许多Reparations。一个维修有一个设备和一个维修公司。

@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
}

就如此容易。

于 2012-05-30T15:07:49.933 回答