尝试在Hibernate中实现继承。
以下是架构
这是什么,类是什么,
//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'时,父表应该被视为单个孩子。当决策者是' X / Y / Z'并且它必须作为父母行事。
但是使用上面的类图,每当决策者是' X / Y / Z'时,4个记录保存在ChildX / ChildY / ChildZ中,而父表中没有记录。
还有如何检索上述记录。
EDITS
@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);
现在我收到一个错误:
应用程序错误:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表&#39; Child&#39;不存在
答案 0 :(得分:0)
当您使用(已加入)继承时,在持久保存子项时,您将始终在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();
}