用于在单个实体的对象之间创建层次结构的关系实体

huangapple go评论61阅读模式
英文:

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 = &quot;ORGANIZATION_LK&quot;)
@AssociationOverrides({
    @AssociationOverride(name = &quot;pk.parentOrganization&quot;, joinColumns = @JoinColumn(name = &quot;PARENT_ID&quot;)),
    @AssociationOverride(name = &quot;pk.childOrganization&quot;, joinColumns = @JoinColumn(name = &quot;CHILD_ID&quot;))})
public class OrganizationRelationship implements Serializable {

  @EmbeddedId
  private OrganizationParentChildPk pk = null;

  @Column
  private String relationshipType = null;

}

Finally, this is my organization entity:

@Entity
@Table(name = &quot;ORGANIZATION&quot;)
public class Organization extends Party {

  @Column(name = &quot;NAME&quot;)
  private String name = null;

  // ???
  private OrganizationRelationship organizationParentRelationship = null;


  @OneToMany(mappedBy = &quot;pk.parentOrganization&quot;, cascade = CascadeType.ALL,
      targetEntity = OrganizationRelationship.class)
  private Set&lt;OrganizationRelationship&gt; 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 = &quot;pk.childOrganization&quot;, cascade = CascadeType.ALL,
      targetEntity = OrganizationRelationship.class)
  private OrganizationRelationship organizationParentRelationship = null;

huangapple
  • 本文由 发表于 2020年4月10日 06:05:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61130981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定