简体   繁体   English

HQL Left Outer以一对一的关系连接空列

[英]HQL Left Outer Join for null column in one-to-one relation

Left outer join is supposed to get all data from left table no matter if there is matching record from B table, however if left tables right_id column is null, records cant be got. 左外连接应该从左表中获取所有数据,无论是否存在来自B表的匹配记录,但是如果左表right_id列为空,则不能获得记录。

I am explaining more 我正在解释更多

In Data model: Order.java, it is my LEFT table, there is a one to one relation 在Data model:Order.java中,它是我的LEFT表,有一对一的关系

@OneToOne(targetEntity = OrderShippingDetail.class, optional=true, cascade = {CascadeType.ALL})
@JoinColumn(name = "SHIPPING_DETAIL_ID", referencedColumnName = "ID")
private OrderShippingDetail shippingDetail;

and HQL is: 和HQL是:

    hql = "SELECT " +
            "o.id as id, " +
            "o.createTime as createTime, " +
            "o.customerEmailAddress as customerEmailAddress, " +
            "o.customerPhoneNumber as customerPhoneNumber, " +
            "o.customerNote as customerNote, " +
            "o.invoicePrintedFlag as invoicePrintedFlag, " +
            "shippingAddress.address.personName as shippingPersonName, " +
            "shippingDetail.shippingCompany.id as shippingCompanyId, "+
            "shippingDetail.shippingCompany.name as shippingCompanyName, "+
            "shippingDetail.receiptNumber as shippingReceiptNumber, "+
            "shippingDetail.trackingNumber as shippingTrackingNumber, "+
            "shippingDetail.price as shippingPrice, "+
            "o.invoiceNumber as invoiceNumber " + 
        "FROM Order AS o " +
        "LEFT OUTER JOIN o.shippingAddress AS shippingAddress " +
        "LEFT OUTER JOIN o.shippingDetail AS shippingDetail ";

But there comes just records which "SHIPPING_DETAIL_ID" is NOT null. 但只有“SHIPPING_DETAIL_ID”不为空的记录。 Is there an error with HQL? HQL有错误吗? It is created by modelling SQL command which is automatically created when hibernate runs. 它是通过建模SQL命令创建的,该命令是在hibernate运行时自动创建的。

I found this, 我找到了这个,

HQL supports two forms of association joining: implicit and explicit. HQL支持两种形式的关联连接:隐式和显式。

The queries shown in the previous section all use the explicit form, that is, where the join keyword is explicitly used in the from clause. 上一节中显示的查询都使用显式形式,即在from子句中显式使用join关键字的位置。 This is the recommended form. 这是推荐的表格。

The implicit form does not use the join keyword. 隐式表单不使用join关键字。 Instead, the associations are "dereferenced" using dot-notation. 相反,使用点符号“解除引用”关联。 implicit joins can appear in any of the HQL clauses. 隐式连接可以出现在任何HQL子句中。 implicit join result in inner joins in the resulting SQL statement. 隐式连接导致生成的SQL语句中的内部联接。

And I remove my dot notation in the SELECT part, so my new HQL: 我删除了SELECT部分​​中的点符号,所以我的新HQL:

hql = "SELECT " +
                "o.id as id, " +
                "o.createTime as createTime, " +
                "o.customerEmailAddress as customerEmailAddress, " +
                "o.customerPhoneNumber as customerPhoneNumber, " +
                "o.customerNote as customerNote, " +
                "o.invoicePrintedFlag as invoicePrintedFlag, " +
                "shippingDetail, " +
                "o.invoiceNumber as invoiceNumber " + 
            "FROM Order AS o " +
            "LEFT OUTER JOIN o.shippingAddress AS shippingAddress " +
            "LEFT OUTER JOIN o.shippingDetail AS shippingDetail ";

So, it works, it returns all records in Order table, however, I dont want to select all columns and relations in the ShippingDetail object. 因此,它工作,它返回Order表中的所有记录,但是,我不想选择ShippingDetail对象中的所有列和关系。 What can I do to solve this issue? 我该怎么做才能解决这个问题?

Add another explicit left join to the query: 向查询添加另一个显式左连接:

SELECT o.id as id, 
...,
shippingCompany.id as shippingCompanyId, 
shippingCompany.name as shippingCompanyName, 
...
FROM Order AS o
LEFT OUTER JOIN o.shippingAddress AS shippingAddress 
LEFT OUTER JOIN o.shippingDetail AS shippingDetail
LEFT OUTER JOIN shippingDetail.shippingCompany AS shippingCompany

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

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