我在使用 Hibernate 时遇到了一个小而奇怪的问题。我有两个表:“SERVICE”和 SERVICE_RELATIONSHIP,它定义了两个服务之间的关系。
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 列被正确插入但不是 RELATIONSHIP。
@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;
}
}
有任何想法吗?提前致谢