JPA self binding is incorrectly defined

I need help with binding to binding to JPA. I think there is something that I have not defined correctly.

I have a JPA bean named ItemEntity. There are two types of items. Parent items and baby items. A parent can have many children, and a child has only one parent. So this is really a multi-valued ManyToOne / OneToMany JPA binding.

In my database, the element 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 element 111, I would expect to see 222, but I get null.

This is how I 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 someone tell me what I'm doing wrong here?

@ManyToOne stg_import_payload. , . , - ?

    "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') ";

.

+4
1

Docs:

javax.persistence.JoinColumn.referencedColumnName()

() , .

, , . OneToMany . JoinTable - , . CollectionTable , . ( , ): , .

:

@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "item_no", referencedColumnName = "parent_item_no")
private ItemEntity parent;

to

@ManyToOne(fetch = FetchType.LAZY, optional = true)
private ItemEntity parent;

+6

All Articles