I´m using wicket and hibernate. I got two objects category
and group
. A group
can have several categorys
and a category
can have several groups
.
My Problem (its pretty hard for me to explain it in english): It seems that in my list I get from the database are equal objects as the size of the categorys I store to the group (while in the database is only ONE group).
Example:
Categorys 1, 2, 3, 4
Group test
test got the category 1 and 2. So in my panel the group test shows twice. If I would add the category 3 the group test would been showed three times.
this is how I get data of my database:
public List<T> getAll( Class theClass) {
List<T> entity = null;
Transaction trns = null;
Session session = sessionFactory.openSession();
try {
trns = session.beginTransaction();
entity = session.createCriteria(theClass).list();
session.getTransaction().commit();
} catch (RuntimeException e) {
e.printStackTrace();
}finally {
session.flush();
session.close();
}
return entity;
}
inside my panel I get a list of my groups like this:
List<Group> groupList = new ArrayList<Group>();
groupList = groupDao.getAll(Group.class);
if I debug through my panel and hold on at this page in groupList
is the SAME object equal to the size of categorys stored to the group. Inside the database is still only ONE row.
Group entity:
@Entity
@Table(name = "GROUP_USER")
public class Group implements Serializable{
@Id
@GeneratedValue
@Column(name = "GROUP_ID")
private int groupID;
@ManyToMany(cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinTable(name="GROUP_TO_CATEGORY",
joinColumns={@JoinColumn(name="GROUP_ID")},
inverseJoinColumns={@JoinColumn(name="CATEGORY_ID")})
private Set<Category> categorys = new HashSet<Category>();
//constructor.. getter and setter..
}
Category entity:
@Entity
@Table(name = "CATEGORY")
public class Category implements Serializable{
@Id
@GeneratedValue
@Column(name = "CATEGORY_ID")
private int categoryId;
@ManyToMany(mappedBy="categorys", fetch = FetchType.EAGER)
private Set<Group> groups = new HashSet<Group>();
//constructor.. getter and setter..
}