我试图弄清楚如何使用 JPA 注释和 Hibernate 配置多对多关系,我阅读了许多文章和论坛讨论,但最终出现错误。
我想知道的第一件事是如何: 配置与属性的多对多关系
我有两个表Equipements和Maintenance_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 ;
}