gpt4 book ai didi

java - Hibernate - 是否需要在父实体中包含 Set 和 OneToMany 注释?

转载 作者:行者123 更新时间:2023-11-29 07:51:05 26 4
gpt4 key购买 nike

我有下面的父实体

@Entity
@Table(name = "EntityA", catalog = "mycatalog")
public class EntityA implements java.io.Serializable {

private int parentId;
private String a;
private Set<EntityB> entityBs = new HashSet<entityB>(0);

public EntityA() {
}

public EntityA(int parentId) {
this.parentId = parentId;
}

public EntityA(int parentId, String a) {
this.parentId = parentId;
this.a = a;
}

@Id
@Column(name = "PARENT_ID", unique = true, nullable = false)
public int getParentId() {
return this.parentId;
}

public void setParentId(int parentId) {
this.parentId = parentId;
}

@Column(name = "columnA", length = 64)
public String getA() {
return this.a;
}

public void setA(String a) {
this.a = a;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "EntityA")
public Set<EntityB> getEntityBs() {
return this.entityBs;
}

public void setEntityBs(Set<EntityB> entityBs) {
this.entityBs = entityBs;
}

和下面的子实体

@Entity
@Table(name = "EntityB", catalog = "mycatalog")
public class EntityB implements java.io.Serializable {

private int childId;
private EntityA entityA;
private String b;


public EntityB() {
}

public EntityB(int childId, EntityA entityA) {
this.eventId = eventId;
this.entityA = entityA;
}

public EntityB(int childId, EntityA entityA, String b) {
this.childId = childId;
this.entityA = entityA;
this.b = b;
}

@Id
@Column(name = "CHILD_ID", unique = true, nullable = false)
public int getChildId() {
return this.childId;
}

public void setChildId(int childId) {
this.childId = childId;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID", nullable = false)
public EntityA getEntityA() {
return this.entityA;
}

public void setEntityA(EntityA entityA) {
this.entityA = entityA;
}

@Column(name = "columnB", length = 64)
public String getB() {
return this.b;
}

public void setB(String b) {
this.b = b;
}
}

我注意到,即使我不在父表中包含 Set 和 OneToMany 批注,查询这些实体仍然有效。请参阅下面没有 Set 的父级:

@Entity
@Table(name = "EntityA", catalog = "mycatalog")
public class EntityA implements java.io.Serializable {

private int parentId;
private String a;


public EntityA() {
}

public EntityA(int parentId) {
this.parentId = parentId;
}

public EntityA(int parentId, String a) {
this.parentId = parentId;
this.a = a;
}

@Id
@Column(name = "PARENT_ID", unique = true, nullable = false)
public int getParentId() {
return this.parentId;
}

public void setParentId(int parentId) {
this.parentId = parentId;
}

@Column(name = "columnA", length = 64)
public String getA() {
return this.a;
}

public void setA(String a) {
this.a = a;
}

}

您能否解释一下区别以及是否以及何时需要将 Set 包含在父表中?

最佳答案

您的父实体与其子实体具有一对多关系。

关系是由连接列映射的,该列总是在多边表中,因此称为拥有边。所以从技术上讲,这种关系是单向的,从 child 到他们的 parent 。

父实体表不包含任何关系信息,因此称为非拥有反向方。如果在父端添加正确注释的 children 字段,JPA 实现可以“模拟”双向关系,但这不是必需的。

如果需要从父对象遍历到子对象,可以添加该字段。如果只是从children遍历到parent,就不要添加。

你可以注意到,非拥有方必须告诉持久化提供者属性名,在拥有方引用它,使用 mappedBy 属性,所以关系可以双向工作对象级别。

关于java - Hibernate - 是否需要在父实体中包含 Set 和 OneToMany 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21143245/

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