I need some help with a JPA self referencing relationship. I think there is something that I have not defined correctly.
I have a JPA entity bean named ItemEntity. There are two types of items. Parent items and child items. The parent item can have many child items and the child item only has one parent item. So really this is a ManyToOne / OneToMany JPA self referencing relationship.
In my database the item table looks like this...
item_no,parent_item_no,item_description
111,null,This is my parent item
222,111,This is my child item
So in my java program when I call itemEntity.getChildren on item 111, I would expect to see 222 however I am getting null.
Here is how I have defined my JPA relationship...
@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;
Can anyone tell me what I am doing wrong here?
The item entity has a @ManyToOne relationship with another one of my entities named stg_import_payload. Here is the named query I am using. Maybe I have to do something special with the named query?
"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') ";
Thanks.