1
我有一個小而奇怪的問題使用休眠。我有兩個表格:「SERVICE」& SERVICE_RELATIONSHIP定義兩個服務之間的關係。如何在Hibernate中使用兩個表來堅持自引用實體?
CREATE TABLE TMS.TB_SERVICE (
ID_SERVICE INTEGER NOT NULL,
NAME VARCHAR(255),
)
CREATE TABLE SERVICE_RELATIONSHIP (
ID_SERVICE INTEGER NOT NULL,
ID_SERVICE_REL INTEGER NOT NULL,
RELATIONSHIP VARCHAR(1) // Could be 'E' (Exclude) or 'I' (Include)
)
我用不同的方法(getInclude和getExclude),以便根據自己有關係的類型來獲得服務。他們工作完美,唯一的問題是當我想要堅持服務,ID_SERVICE & ID_SERVICE_REL列插入正確但不是關係。
@Table(name="SERVICE")
public class Service implements Serializable {
private String name;
private Integer id;
private Collection<Service> exclude;
private Collection<Service> include;
[..]
@JoinTable(name = "SERVICE_RELATIONSHIP",
joinColumns = {
@JoinColumn(name="ID_SERVICE", unique = false, updatable = true)
},
inverseJoinColumns = {
@JoinColumn(name="ID_SERVICE_REL")
}
)
@WhereJoinTable(clause = "RELATIONSHIP='E'")
public Collection<Service> getExclude() {
return exclude;
}
[..]
@OneToMany(fetch=FetchType.EAGER)
@JoinTable(name = "SERVICE_RELATIONSHIP",
joinColumns = {
@JoinColumn(name="ID_SERVICE", unique = false, updatable = true)
},
inverseJoinColumns = {
@JoinColumn(name="ID_SERVICE_REL")
}
)
@WhereJoinTable(clause = "RELATIONSHIP='I'")
public Collection<Service> getInclude() {
return include;
}
}
任何想法? 在此先感謝