我需要一些JPA自引用關係的幫助。我認爲有些東西我沒有正確定義。JPA自引用關係定義不正確
我有一個名爲ItemEntity的JPA實體bean。有兩種類型的項目。父項目和子項目。父項可以有許多子項,而子項只有一個父項。所以真的這是一個ManyToOne/OneToMany JPA自引用關係。
在我的數據庫中的項目表看起來像這樣...
item_no,parent_item_no,item_description
111,null,This is my parent item
222,111,This is my child item
所以,當我打電話itemEntity.getChildren上項目111我的java程序,我希望看到222但是我得到空。
下面是我定義我的JPA關係......
@Entity(name = "stg_item")
public class ItemEntity implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "itemid")
@TableGenerator(name = "itemid", table = "stg_items_sequence", allocationSize = 1)
private Long id;
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "item_no", referencedColumnName = "parent_item_no")
private ItemEntity parent;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "parent")
private Collection<ItemEntity> children;
誰能告訴我,我做錯了什麼嗎?
項目實體與另一個名爲stg_import_payload
的實體之間存在@ManyToOne
關係。這是我正在使用的命名查詢。也許我必須對命名查詢做一些特別的事情?
"SELECT x "
+ " FROM stg_import_payload x "
+ " WHERE x.processedInd='N' "
+ " AND EXISTS (SELECT stg_item FROM stg_item stg_item WHERE stg_item.importPayload = x AND stg_item.processedInd='N') ";
謝謝。