i cannot use sorting on join tables. Let me explain;
i have three tables. users, roles and user_roles.
my JPA entities are User, UserRole, UserRolePK, Role.
|User | | UserRole | | UserRolePK | | Role |
|--------| |----------| -------------- --------
|id | | pk | | user | | id |
|name | | role | | name |
in fact the output that i want is:
“SELECT * FROM user_roles ur JOIN users u ON u.ID = ur.UserID ORDER BY u.name;”
so i try to use hibernate criteria API.
CriteriaImpl criteria = (CriteriaImpl) session.createCriteria(UserRole.class);
criteria.addOrder(Order.asc("pk.user.name"));
List userRoles = criteria.list();
The error is
could not resolve property: pk.user.name of: models.UserRole
how can i use criteria API on join tables?
If a class references other classes you can not simply access their properties in Restrictions and Orders. You will have to create an alias for the referenced objects and then use the alias to define Restrictions and Orders on the other objects.
The alias definitions will create a join with the tables of the other classes. Only properties that are directly mapped to the table of the class or an alias can be used in Restrictions or Orders. If your class contains components they can be accessed directly without an alias.