java 休眠条件api连接表问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3627550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate criteria api join table problem
提问by F?rat Kü?üK
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_roles。我的 JPA 实体是 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;"
事实上,我想要的输出是:“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.
所以我尝试使用休眠条件 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
错误是无法解析属性:pk.user.name of:models.UserRole
how can i use criteria API on join tables?
如何在连接表上使用标准 API?
回答by Reboot
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.
如果一个类引用其他类,您不能简单地在限制和命令中访问它们的属性。您必须为引用的对象创建一个别名,然后使用该别名来定义其他对象的限制和顺序。
Criteria criteria = (CriteriaImpl) session.createCriteria(UserRole.class);
criteria.createAlias("pk", "a1");
criteria.createAlias("a1.user", "a2");
criteria.addOrder(Order.asc("a2.name"));
List userRoles = criteria.list();
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.
别名定义将创建与其他类的表的连接。只有直接映射到类或别名的表的属性才能用于 Restrictions 或 Orders。如果您的类包含组件,则无需别名即可直接访问它们。
回答by Tom
Well, first of all I think we should see your entities mappings (Java Classes)
好吧,首先我认为我们应该看到您的实体映射(Java 类)
On the other hand, i'll assume your UserRole class has a reference to a User instance.
另一方面,我假设您的 UserRole 类具有对 User 实例的引用。
@Entity
@Table (name="UserRole")
public class UserRole{
User user;
@OneToMany //correct mapping here
public User getUser(){return this.user;}
}
You should use createCriteria
to navigate the association.
您应该使用createCriteria
来导航关联。
public List<UserRole> getRolesWithUser(){
Criteria userRoleCriteria = session.createCriteria(UserRole.class);
userRoleCriteria.createCriteria("user"); //joins the table.
userRoleCriteria.addOrder("user.name"); //adds order
return criteria.list();
}
The problem with this is that it will bring instances of UserRole
, but you can navigate to User
.
这样做的问题是它会带来 的实例UserRole
,但您可以导航到User
.
List<UserRole> userRoles = getRolesWithUser();
for (UserRole role:userRoles){
User user = role.getUser();
//do something with role , user
}
Take a look at the Criteria API. Its always helpful !
看一看Criteria API。它总是有帮助的!
回答by fasseg
quite easy just use createCriteria on the associations:
非常简单,只需在关联上使用 createCriteria 即可:
Criteria c=session.createCriteria(User.class)
.createCriteria("roles")
.add(Restrictions.eq("name","test")
.list();
This would join two tables for the Entites User and their associated Collection "roles". and would return all users which have a role associated with themselves called "test".
这将为实体用户及其关联的集合“角色”连接两个表。并将返回所有与自己相关的角色的用户,称为“测试”。
hope that helped
希望有所帮助