2012-05-30 4 views
0

JPA 어노테이션과 하이버 네이트를 사용하여 다 대다 관계를 구성하는 방법을 알아 내려고했지만, 많은 기사와 포럼 토론을 읽었지만, 오류.애트리뷰트를 가진 Many to Many 관계

내가 두 테이블 EQUIPEMENTSMaintenance_Companies과의 관계 배상

이 속성을 가진 많은 관계로 많은 구성 :

내가 알고 싶어 우선은 어떻게 관계에 대한 엔티티를 추가하고 다른 일부에서는 @OneToMany를 만듭니다. 이렇게 구성된 기본 키에 문제가있어서 Collection 접근 방식을 사용했지만 예외가 있습니다. :

org.hibernate.MappingException: Repeated column in mapping for collection: mmapp.domain.Equipement.reparations column: EQUIPEMENT_FK

EQUIPEMENT :

@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" , 
    [email protected](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" , 
    [email protected](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 ; 
} 

답변

3

이 매핑은 MU가되지 않습니다 당신은 처음부터 당신이 원했던 것과 멀리 떨어져 있습니다. 속성과의 많은 관계.

many-to-many는 두 엔티티 사이에서 조인 테이블을 사용합니다. 이 조인 테이블에 두 개 이상의 ID가 있으면 조인 테이블이 아니라 다른 엔티티로 매핑되어야하며 자체 생성 된 ID가있는 기능적 엔티티입니다.

  • 장비
  • MaintenanceCompany

한 장비는 많은 배상금을 가지고

  • 배상 :

    당신은 따라서 3 개 단체, 자체 생성 된 ID와 각을 가져야한다. 하나의 유지 보수 회사에는 많은 보상이 있습니다. 하나의 보상은 장비 1 대와 유지 보수 회사 1 대가 있습니다.

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

    간단합니다.