[英]What's the difference between these two ways of queries in hibernate?
I recently dig into Hibernate and found two ways to do an inner join query using HQL, but I don't know what's the difference between them and which one to use. 最近,我深入研究了Hibernate,发现了两种使用HQL进行内部联接查询的方法,但我不知道它们之间的区别以及所使用的区别。
Let's assume that we have two tables: 假设我们有两个表:
Table user
+-------+-----------+-----------+
|userId |userName | userLevel |
+-------+-----------+-----------+
|1001 |James | 0 |
+-------+-----------+-----------+
|1002 |Tom | 2 |
+-------+-----------+-----------+
Table order
+----------+-----------+-----------+----------+
|orderId |userId |productId |fee |
+----------+-----------+-----------+----------+
|1001 |1002 |1003 | 5 |
+----------+-----------+-----------+----------+
|1002 |1002 |1005 | 50 |
+----------+-----------+-----------+----------+
|1003 |1001 |1010 | 30 |
+----------+-----------+-----------+----------+
User Tom could have multi order records in the order
table. 用户Tom在
order
表中可能有多个订单记录。 And here comes a demand that we want to find some the order information of Tom alone with his name, if it is raw sql
we could do this: 这是一种需求,我们想要单独查找一些Tom的订单信息及其名字,如果它是原始
sql
则可以执行以下操作:
select user.userId, user.userName, order.orderId, order.fee from user join order where user.userId=order.userId and user.userId=1002;
But on coming to hibernate I found two ways that could achieve this: 但是进入休眠状态后,我发现了两种方法可以实现此目的:
using the one-to-many
mapping to connect the relation between user and order and make a orderEntities<Set>
in the userEntity
definition then make a HQL query like this: 使用
one-to-many
映射连接用户和订单之间的关系,并在userEntity
定义中创建orderEntities<Set>
,然后进行如下HQL查询:
FROM UserEntity as U INNER JOIN U.orderEntities as O WHERE U.userId=1002;
Or 要么
omit the one-to-many
definition and just make a HQL query like this: 省略
one-to-many
定义,只需要像这样进行HQL查询:
FROM UserEntity U, OrderEntity O WHERE U.userId = O.userId AND U.userId=1002;
What's the different between these two ways of querying? 这两种查询方式有何不同? And which one should I use?
我应该使用哪一个呢?
Both are wrong, and BTW, the SQL query is wrong as well. 两者都是错误的,顺便说一句,SQL查询也是错误的。
The first one has an inner join that is useless. 第一个内部连接无效。 The only effect of the join is that it will cause the query to not return anything if the user doesn't have any order.
联接的唯一作用是,如果用户没有任何顺序,它将导致查询不返回任何内容。 If the goal is to retrieve the user by ID, and then to access its orders, then you shouldn't even use a query:
如果目标是通过ID检索用户,然后访问其订单,那么您甚至不应使用查询:
UserEntity u = em.find(UserEntity.class, 1002);
Set<OrderEntity> orders = u.getOrderEntities();
That will however execute two queries: one to load the user, and a second one to load the orders if you actually do something with them (like iterate on them, getting the size of the set, or whatever) 但是,这将执行两个查询:一个查询加载用户,第二个查询加载订单(如果您实际对他们执行某项操作(例如迭代它们,获取集合的大小或其他))
If the goal is to load the user and its orders in a single query, then the query should be 如果目标是在单个查询中加载用户及其订单,则该查询应为
select distinct u from UserEntity u
left join fetch u.orderEntities
where u.id = 1002
Note the usage of a left join, and the usage of fetch . 注意左联接的用法和fetch的用法。
The second one is wrong because it uses O.userId
, which should not exist. 第二个是错误的,因为它使用了
O.userId
,它不应该存在。 If the order entity needs to know its user, then you should have a ManyToOne association from Order to User. 如果订单实体需要了解其用户,则您应该具有从订单到用户的ManyToOne关联。 Never a field holding the ID of another entity.
永远不要拥有另一个实体ID的字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.