2016-12-05 86 views
0

我在項目中有一個奇怪的需求。加入兩個n:m +屬性表(我將用虛擬屬性呈現行爲)。嵌套n:m +屬性JPA

  1. FirstTable(idPlace,idAddress,idSchool,wage)joined 1:m;
  2. SecondTable(idPlace,idAddress,idSchool,數量,idEnterprise)

EDIT(例如模式): enter image description here

當然,我有表地點,住址,學校,企業與他們各自的在實體類中實現的Ids,獲取,集合和屬性。

CODE:

廣場

@Entity 
@Table(name = "Place") 
public class Place implements Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "idLine") 
private Long idLine; 

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.place") 
private List<FirstTable> firstTables; 
} 

地址

@Entity 
@Table(name = "Address") 
public class Address implements Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "idAddress") 
private Long idAddress; 

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.address") 
private List<FirstTable> firstTables; 
} 

學校

@Entity 
@Table(name = "School") 
public class School implements Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "idSchool") 
private Long idSchool; 

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.school") 
private List<FirstTable> firstTables; 
} 

FirstTable

@Entity 
@Table(name = "FirstTable") 
@AssociationOverrides({ @AssociationOverride(name = "pk.school", joinColumns = @JoinColumn(name = "idSchool")), 
    @AssociationOverride(name = "pk.address", joinColumns = @JoinColumn(name = "idAddress")), 
    @AssociationOverride(name = "pk.place", joinColumns = @JoinColumn(name = "idPlace")) }) 
public class FirstTable implements Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

@EmbeddedId 
protected FirstTablePK pk = new FirstTablePK(); 
} 

FirstTablePK

@Embeddable 
public class FirstTablePK implements Serializable{ 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
@ManyToOne 
private Address address; 
@ManyToOne 
private Place place; 
@ManyToOne 
private School school; 
} 

上面提到的表和聯接是完全工作。現在我想加入First Table和Second Table。

企業

@Entity 
@Table(name = "Enterprise") 
public class Enterprise implements Serializable { 

private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "idEnterprise") 
private Long idEnterprise; 

@OneToMany(取= FetchType.LAZY,級聯= CascadeType.ALL,的mappedBy = 「pk.enterprise」) 私人列表secondTables;現在 }

爲SecondTable我已經遵循同樣的邏輯,其連接到企業。爲了與FirstTable連接我試過這樣:

@Entity 
@Table(name = "SecondTable") 
@AssociationOverrides({ 
     @AssociationOverride(name = "pk.firstTable", joinTable = @JoinTable(
       name = "FirstTable", inverseJoinColumns = { 
       @JoinColumn(name = "idSchool", referencedColumnName = "idSchool"), 
       @JoinColumn(name = "idAddress", referencedColumnName = "idAddress"), 
       @JoinColumn(name = "idPlace", referencedColumnName = "idPlace") })), 
     @AssociationOverride(name = "pk.enterprise", joinColumns = @JoinColumn(name = "idEnterprise")) }) 
public class SecondTable implements Serializable{} 

自己是不是在我的註釋工作,我試圖做一個inverseJoin到FirstTable表。彙編顯示此錯誤:

"org.hibernate.AnnotationException: A component cannot hold properties split into 2 different tables" 

我試圖提供一個MV示例。 在此先感謝,我真的需要你的幫助。

回答

0

幾小時後,許多嘗試之前,我已經設法解決問題。事實上,我最初想的解決方案要簡單得多。

這就是:

@AssociationOverride(name = "pk.firstTable", joinColumns = { 
      @JoinColumn(name = "idSchool"), 
      @JoinColumn(name = "idAddress"), 
      @JoinColumn(name = "idPlace") }), 
    @AssociationOverride(name = "pk.enterprise", joinColumns = @JoinColumn(name = "idEnterprise")) })