0
額外的列的關係我有3個數據表:
應用 {id_app,版本,名稱}
客戶 {org_id,名}
協會 {id_app,版本,org_id,狀態}。
應用程序具有複合主鍵(id_app,版本),Customers的主鍵是org_id,關聯具有複合主鍵(id_app,version,org_id)。許多一對多複合主鍵,在Hibernate中
在我的Java應用程序,我有以下類別:
@Entity
@Table(name = "Applications")
@IdClass(ApplicationId.class)
public class Application implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID_APP", nullable = false)
private String idApp;
@Id
@Column(name = "VERSION", nullable = false)
private String version;
@Column(name = "NAME")
private String name;
@OneToMany(mappedBy = "idPk.appPk", fetch = FetchType.LAZY) // cascade = CascadeType.ALL)
private List<Association> custApps;
// getters and setters
}
public class ApplicationId implements Serializable {
private static final long serialVersionUID = 1L;
private String idApp;
private String version;
//hashcode and equals
}
@Entity
@Table(name = "CUSTOMERS")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ORG_ID", unique = true, nullable = false)
private Integer orgID;
@Column(name = "NAME")
private String name;
@OneToMany(mappedBy="idPk.customerPk", fetch = FetchType.LAZY)
private List<Association> custApps;
//getters and setters
}
@Entity
@Table(name = "ASSOCIATIONS")
@AssociationOverrides({
@AssociationOverride(name = "idPk.appPk", joinColumns = @JoinColumn(name = "ID_APP")),
@AssociationOverride(name = "idPk.appPk", joinColumns = @JoinColumn(name = "VERSION")),
@AssociationOverride(name = "idPK.customerPk", joinColumns = @JoinColumn(name = "ORG_ID"))
})
public class Association implements Serializable {
private static final long serialVersionUID = 1L;
private AssociationId idPk = new AssociationId();
private String state;
public Association() {
super();
}
@EmbeddedId
public AssociationId getIdPk() {
return idPk;
}
@Transient
public Customer getCustomerPk() {
return idPk.getCustomerPk();
}
@Transient
public Application getAppPk() {
return idPk.getAppPk();
}
@Column(name = "STATE")
public String getState() {
return state;
}
//setters , hashCode and equals
}
@Embeddable
public class AssociationId implements Serializable {
private static final long serialVersionUID = 1L;
private Application appPk;
private Customer customerPk;
// here is the problem ?
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumns({ @JoinColumn(name = "ID_APP", referencedColumnName = "ID_APP"),
@JoinColumn(name = "VERSION", referencedColumnName = "VERSION") })
public Application getAppPk() {
return appPk;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="ORG_ID")
public Customer getCustomerPk() {
return customerPk;
}
//setter, hashCode and equals
}
什麼是正確的註釋?這種關係在應用程序和客戶之間是多對多的,我爲此創建了關聯表,併爲額外的列「狀態」創建了關聯表。
現在我收到這個錯誤:引用來自sla.model.Association的sla.model.Application的外鍵具有錯誤的列數。應該是2。
請幫忙。
http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite - 關聯 –
這不是我的情況,我嘗試了類似的東西,但它不起作用,因爲我在應用程序表 – SimLine
上也有一個複合主鍵請再次閱讀。我鏈接到的部分說:*在下面的示例中,PersonAddress實體標識符由兩個@ManyToOne關聯*組成。這**完全**你有什麼。上面的例子顯示了各種其他解決方案,所有解決方案都有一個ManyToOne關聯作爲標識符的一部分。 –