简体   繁体   中英

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.

I am explaining more

In Data model: Order.java, it is my LEFT table, there is a one to one relation

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

and HQL is:

    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. Is there an error with HQL? It is created by modelling SQL command which is automatically created when hibernate runs.

I found this,

HQL supports two forms of association joining: implicit and explicit.

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. This is the recommended form.

The implicit form does not use the join keyword. Instead, the associations are "dereferenced" using dot-notation. implicit joins can appear in any of the HQL clauses. implicit join result in inner joins in the resulting SQL statement.

And I remove my dot notation in the SELECT part, so my new 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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