2017-01-18 61 views
1

試圖在Hibernate中實現繼承。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個父記錄應該是a dded
  • 如果決定者是'X/Y/Z',並且需要保存4條記錄,那麼應該添加1條父記錄和4條ChildX/ChildY/ChildZ記錄。

enter image description here

然而,父表應該被視爲一個孩子時,決勝局是「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); 

現在我得到一個錯誤:

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

+0

孩子們不應該延長家長嗎?如果'''Parent'''不能獨立存在,你應該把它抽象出來。 –

+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(); 
} 
+0

我嘗試了..錯誤是表格孩子不存在。由於沒有表'子' –

+0

實體沒有問題,所以如何創建表?此外,您需要指定何時出現錯誤,以及是否持續存在時,代碼的外觀如何。我將編輯我的答案,以包含JPA以保存父項和特定的子項。 –

+0

更新了我的問題......謝謝 –