I need 2 lists in my main Hibernate object: one for layouts and one for items. A layout may contain any item from main object, even if it's used by another layouts.
I use following POJO:
Code:
public class MyProject {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name = "myProject_Id")
@OrderColumn(name="items_index")
private List<MyItem> items = new ArrayList<>();
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name = "myProject_Id")
@OrderColumn(name="itemsLayout_index")
private List<MyLayout> layouts = new ArrayList<>();
}
public class MyItem {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name = MyItem_Id")
@OrderColumn(name="subitem_index")
private List<MySubItem> subitems = new ArrayList<>();
}
public class MyLayout {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name = MyLayout_Id")
@OrderColumn(name="layout_index")
private List<MyItem> items = new ArrayList<>();
}
But it causes NonUniqueObjectException during saving MyLayout elems, because of subitems.
Is there any way to make such a structure without link objects?