in my user table i have two users
when trying to select all users with HQL query as follows:
getCurrentSession().createQuery("From User").list();
it returns the two elements, and that’s right.
but when using createCriteria as follows:
getCurrentSession().createCriteria(User.class).list();
it returns 36 element (the two elements repeated)!!!!
any ideas why i am getting such behavior with createCriteria, is that related to hibernate relations ? because in User domain i have ManyToMany relation with Role domain, is it related to session factory or hibrenate version or hibernate configuration ?
relation:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
private Set<Role> roles = new HashSet<Role>(0);
i am confused, please advise, thanks.
You have a Many-to-many relation with eager fetching. Criteria query will be translated into a SELECT with joins in it, and so in the result you’ll have many rows for each User.
To remove duplicates add all the results to a HashSet, or use DistinctRootEntityResultTransformer.