简体   繁体   English

基于另一个表中的数据从一个表中获取数据的正确的Hibernate查询是什么?

[英]What is the proper Hibernate Query for fetching data from one table based on data in another table?

I have two tables, Device contains details of a device, including if it is assigned, and CustDevice which contains additional information for devices that have been assigned to a customer. 我有两个表,“设备”包含设备的详细信息(包括是否已分配),以及“ CustDevice”,其中包含已分配给客户的设备的其他信息。

I need to select records from the Device table if it has been assigned to a particular customer. 如果已将其分配给特定客户,我需要从设备表中选择记录。

My first naive stab at a query was: 我在查询中的第一个幼稚的刺是:

from Device where Device.deviceId = CustDevice.deviceId and Device.status=2 and CustDevice.accountId=33 来自设备的设备,其中Device.deviceId = CustDevice.deviceId和Device.status = 2以及CustDevice.accountId = 33

That obviously does not work, but it explains the relationship I am trying to do. 那显然是行不通的,但是它解释了我正在尝试建立的关系。 I attempted to add a join in the mix, but I do not know the proper syntax for joining two tables. 我试图在混合中添加联接,但是我不知道联接两个表的正确语法。

I tried: 我试过了:

from Device dev join dev.deviceId CustDevice where dev.deviceId = CustDevice.deviceId and dev.deviceStatus = 2 and CustDevice.accountId=33 来自Device dev加入dev.deviceId CustDevice,其中dev.deviceId = CustDevice.deviceId和dev.deviceStatus = 2且CustDevice.accountId = 33

But that doesn't work either. 但这也不起作用。 Can anyone help me out with this query?? 谁能帮我这个查询? Thanks 谢谢

This sounds like a left join, that is all of a table plus the joined table if it is referenced. 这听起来像是左联接,即是一个表加上被引用表(如果已引用)的全部。

Here is and article on left joins in Hibernate. 这是Hibernate中左联接的文章。

http://www.jairrillo.com/blog/2009/01/29/how-to-use-left-join-in-hibernate-criteria/ http://www.jairrillo.com/blog/2009/01/29/how-to-use-left-join-in-hibernate-criteria/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM