2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

hibernate criteria api join table problem

i cannot use sorting on join tables. Let me explain;

i have three tables. users, groups and user_groups. my JPA entities are User, UserGroup, UserGroupPK, Groups.


in fact the output that i want is: "SELECT * FROM user_groups ug JOIN users u ON u.ID = ug.UserID ORDER BY u.name;"

so i try to use hibernate criteria API.

CriteriaImpl criteria = (CriteriaImpl) session.createCriteria(UserGroup.class);
criteria.addOrder(Order.asc("pk.user.name"));
List userGroups = criteria.list();

The error is could not resolve property: pk.user.name of: models.UserGroup

how can i use criteria API on join tables?
userimage

it is quite easy just use createCriteria on the associations:
Criteria c=session.createCriteria(Users.class)
                  .createCriteria("groups")
                  .add(Restrictions.eq("name","test")
                  .list();


Hope this will help  you.


Answer is