gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:30:30 24 4
gpt4 key购买 nike

我有一个可以有单亲和/或单子(monad)的组织实体。组织之间的层次结构需要有额外的列(例如关系类型),因此我创建了一个实体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表?

预先感谢您的帮助。

最佳答案

我设法使用以下注释让它工作:

  @OneToOne(mappedBy = "pk.childOrganization", cascade = CascadeType.ALL,
targetEntity = OrganizationRelationship.class)
private OrganizationRelationship organizationParentRelationship = null;

关于java - 关系实体用于在单个实体的对象之间创建层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61130981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com