英文:
Relationship entity used to create a hierarchy between objects of a single entity
问题
我有一个名为"Organization"的实体,可以有一个单一的父实体和/或子实体。组织之间的层次关系需要有额外的列(比如关系类型),因此我创建了一个名为"OrganizationRelationship"的实体,用于表示父子关系。我已经成功地使用OneToMany关联来处理子实体,但是在处理父实体时遇到了问题(我想应该使用ManyToOne关联)。
我有一个可嵌入的主键如下所示:
@Embeddable
public class OrganizationParentChildPk implements Serializable {
@ManyToOne(cascade = CascadeType.ALL)
public Organization parentOrganization;
@ManyToOne(cascade = CascadeType.ALL)
public Organization childOrganization;
}
然后是关系实体:
@Entity
@Table(name = "ORGANIZATION_LK")
@AssociationOverrides({
@AssociationOverride(name = "pk.parentOrganization", joinColumns = @JoinColumn(name = "PARENT_ID")),
@AssociationOverride(name = "pk.childOrganization", joinColumns = @JoinColumn(name = "CHILD_ID"))})
public class OrganizationRelationship implements Serializable {
@EmbeddedId
private OrganizationParentChildPk pk = null;
@Column
private String relationshipType = null;
}
最后,这是我的组织实体:
@Entity
@Table(name = "ORGANIZATION")
public class Organization extends Party {
@Column(name = "NAME")
private String name = null;
// ???
private OrganizationRelationship organizationParentRelationship = null;
@OneToMany(mappedBy = "pk.parentOrganization", cascade = CascadeType.ALL,
targetEntity = OrganizationRelationship.class)
private Set<OrganizationRelationship> organizationChildRelationship = null;
}
我希望父关系字段也能够使用相同的"ORGANIZATION_LK"表,如果可能的话。
提前感谢您的帮助。
英文:
I have an Organization entity which can have a single parent and/or children. The hierarchy between organizations need to have extra columns (such as relationship type), so I created an entity OrganizationRelationship which is used for parents and also for children. I was able to get the children working with a OneToMany association but couldn't get the parent to work (which should use ManyToOne I guess).
I have an embeddable primary key as follows:
@Embeddable
public class OrganizationParentChildPk implements Serializable {
@ManyToOne(cascade = CascadeType.ALL)
public Organization parentOrganization;
@ManyToOne(cascade = CascadeType.ALL)
public Organization childOrganization;
}
And then the relationship entity:
@Entity
@Table(name = "ORGANIZATION_LK")
@AssociationOverrides({
@AssociationOverride(name = "pk.parentOrganization", joinColumns = @JoinColumn(name = "PARENT_ID")),
@AssociationOverride(name = "pk.childOrganization", joinColumns = @JoinColumn(name = "CHILD_ID"))})
public class OrganizationRelationship implements Serializable {
@EmbeddedId
private OrganizationParentChildPk pk = null;
@Column
private String relationshipType = null;
}
Finally, this is my organization entity:
@Entity
@Table(name = "ORGANIZATION")
public class Organization extends Party {
@Column(name = "NAME")
private String name = null;
// ???
private OrganizationRelationship organizationParentRelationship = null;
@OneToMany(mappedBy = "pk.parentOrganization", cascade = CascadeType.ALL,
targetEntity = OrganizationRelationship.class)
private Set<OrganizationRelationship> organizationChildRelationship = null;
}
What I would like is to make the parent relationship field also use the same ORGANIZATION_LK table if that is possible?
I thank you in advance for your help.
答案1
得分: 1
我成功地使用以下注释使其工作起来:
@OneToOne(mappedBy = "pk.childOrganization", cascade = CascadeType.ALL,
targetEntity = OrganizationRelationship.class)
private OrganizationRelationship organizationParentRelationship = null;
英文:
I managed to get it working using the following annotation:
@OneToOne(mappedBy = "pk.childOrganization", cascade = CascadeType.ALL,
targetEntity = OrganizationRelationship.class)
private OrganizationRelationship organizationParentRelationship = null;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论