I am trying to create an Entity that has a Parent and children from the same type as he is (like a tree).
here is the code:
@Entity @Table(name = "areas") public class Area {
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@Column(name = "name")
private String name;
@Column(name = "area_type", nullable = true)
private int areaType;
@ManyToOne
@JoinColumn(name="parent_area_id")
private Area parentArea;
@OneToMany (fetch = FetchType.EAGER, mappedBy = "parentArea")
@Cascade({CascadeType.ALL})
private Collection<Area> childAreas = new ArrayList<Area>();
i have seen this code in a few posts in stack overflow first of all when i try to save such entity to the database i get:
nested exception is org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
furthermore, there is something i don't understand fully about this design.
if the children areas are mapped by the parent areas. wont it mean that "this" area will be among the children fached? i other words... wont the children areas be the children of the parent area of "this" class.
thanks.