I know this is very basic question, but i dont seem to find answer for it. i know how to do this with mapping files but i want to learn hibernate annotations.
I have two classes:
Code:
@Entity
public class Forum {
...
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@OneToMany(cascade = CascadeType.ALL, mappedBy="forum")
@OrderBy("order")
@Fetch(FetchMode.JOIN)
private List<Area> areas;
...
}
@Entity
public class Area {
...
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@ManyToOne
public Forum getForum() {
return forum;
}
...
}
they also have equals methods that make them equal by id
i have persited 3 forums, 1st with 2 areas, 2nd with one, and 3rd with 2.
i'll try getting these 3 Forums with every one correctly setted Areas with
Code:
Criteria criteria = session.createCriteria(Forum.class)
.setFetchMode("areas", FetchMode.JOIN)
.addOrder(Order.asc("order"));
List list = criteria.list();
but that list contains 5 Forums. 2x Forum 1, Forum 2 and 2x Forum 3.
there is created as many forums as there is areas. and every forum contains correct amount of areas. both copies of Forum 1 has 2 areas and so on.
Hibernate version: 3.2
Hibernate Annotations version: 3.3.0 GA
Name and version of the database you are using: MySql 5.0
The generated SQL (show_sql=true): this returns every area joined with correct forum row