简体   繁体   English

休眠中这两种查询方式有何区别?

[英]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: 但是进入休眠状态后,我发现了两种方法可以实现此目的:

  1. 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 要么

  1. 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.

相关问题 这两种从LocalDateTime创建DateTime的方法有什么区别? - What's the difference between these two ways of creating a DateTime from a LocalDateTime? 以下两种创建可执行JAR的方法有什么区别? - What's the difference between the following two ways to create an executable JAR? 这两种方法在Java中初始化字段有什么区别? - what's the difference between those two ways to initialize fields in Java? 这两种初始化HashMap的方式有什么区别? - What is the difference between these two ways of initializing a HashMap? 用这两种方式指定一个类有什么区别? - What is the difference between specifying a class in these two ways? Java中这两种投射方式有什么区别? - What is the difference between these two ways of casting in Java? 这两种初始化String的方法有什么区别? - What is the difference between these two ways of initializing a String? 这两种处理异常的方式有什么区别 - What is the difference between these two ways of handling an exception 这些初始化HashMap的方法有什么区别? - What's the difference between these ways of initializing a HashMap? 这两种从现有List创建新ArrayList的方法有何不同? - What's the difference between these two ways of creating an new ArrayList from existing List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM