gpt4 book ai didi

java - Hibernate继承,父类应该根据决策者是子类还是父类

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:01 25 4
gpt4 key购买 nike

尝试在Hibernate中实现继承。

以下是架构

enter image description here

这是什么,类是,

//Grand Parent Class
@Entity
@Table(name="grand_parent")
public class GrandParent{//consider @id}

//Parent Class
@Entity
@Table(name = "parent")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "decider", discriminatorType = DiscriminatorType.STRING)
public class Parent{//consider @id}

//ChildX class
@Entity
@Table(name = "childX")
@PrimaryKeyJoinColumn(name="id")
@DiscriminatorValue("X")
public class ChildX() extends Parent{//consider value}

//ChildY class
@Entity
@Table(name = "childY")
@PrimaryKeyJoinColumn(name="id")
@DiscriminatorValue("Y")
public class ChildY extends Parent(//consider value){}

//ChildZ class
@Entity
@Table(name = "childZ")
@PrimaryKeyJoinColumn(name="id")
@DiscriminatorValue("Z")
public class ChildZ() extends Parent{//consider value}

用例:

  • 如果决策者为“K”,并且需要保存 4 条记录,则应添加 4 条父记录
  • 如果决策者为“X/Y/Z”,需要保存 4 条记录,则应添加 1 条父记录和 4 条 ChildX/ChildY/ChildZ 记录。

enter image description here

但是,当决策者为“K”时,父表应被视为单个子表,并且当决策者为“X/Y/Z”时,它必须充当父表

但在上面的类图中,每当决策者为“X/Y/Z”时,ChildX/ChildY/ChildZ 中保存了 4 条记录,而父表中没有记录。

还有如何检索上述记录。

编辑

@Entity
@Table(name="grand_parent")
public class GrandParent{
@OneToMany(mappedBy = "parentRecord",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.DELETE)
private List<parent> parentList;
}

//Parent Class
@Entity
@Table(name = "parent")
public class Parent{
@ManyToOne()
@JoinColumn(name = "fk_gp_id")
private GrandParent parentRecord;

@OneToMany(mappedBy = "childrecord",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.DELETE)
private List<Child> childList;
}

@Entity
@DiscriminatorColumn(name = "decider", discriminatorType = DiscriminatorType.STRING)
public abstract class Child(){
@ManyToOne(optional = false)
@JoinColumn(name = "fk_parent_id")
private parent childrecord;
}

//ChildX class
@Entity
@Table(name = "childX")
@DiscriminatorValue("X")
public class ChildX() extends Parent{//consider value}
......

添加..

GrandParent gp = new GrandParent();
Parent p = new Parent();
ChildX ch = new ChildX();
ch.setChildrecord(p);
p.setChildList(//Array added ch);
p.setParentRecord(gp);
gp.setParentList(//Array added p);
persist(gp);

现在我收到错误:

Application error : com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'Child' doesn't exist

最佳答案

当您使用(联合)继承时,当您保留子项时,您始终会在父表和子表中插入一行。这意味着每个 parent 最多有一个 child 。在您的表图中,您有多个具有相同父级的子级,这不是继承,这是一个 OneToMany 实体关系,这由您的 ER 图确认。

我的猜测是您正在寻找这样的东西:

@Entity
public class Parent {
@Id
private long id;

@OneToMany(mappedBy = "parent")
private Collection<Child> children;
}


@Entity
@DiscriminatorColumn(name = "decider", discriminatorType = DiscriminatorType.STRING)
public abstract class Child {
@Id
private long id;

@ManyToOne
private Parent parent;
// other common fields
}

@Entity
@DiscriminatorValue("X")
public class ChildX extends Child {
// specific fields for child x
}

// more children types

一个父级可以有多个子级,并且这些子级可以是不同的类型。子基类具有引用父级的外键列,根据我的经验,继承基类通常最终是抽象的。

编辑:以下是存储父级和特定子级的代码示例。

EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
Parent p = new Parent();
ChildX childX = new ChildX();
childX.setParent(p);
em.persist(p);
em.persist(childX);
em.getTransaction().commit();
} finally {
em.close();
}

关于java - Hibernate继承,父类应该根据决策者是子类还是父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41721745/

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