0

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.

Gleeb
  • 10,773
  • 26
  • 92
  • 135
  • see http://stackoverflow.com/questions/4334970/hibernate-cannot-simultaneously-fetch-multiple-bags – tibtof May 30 '12 at 13:30

1 Answers1

2

This seems like a similar problem. You should try:

@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany (mappedBy = "parentArea")
@Cascade({CascadeType.ALL})
private Collection<Area> childAreas = new ArrayList<Area>();
Community
  • 1
  • 1
tibtof
  • 7,857
  • 1
  • 32
  • 49