Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 447
efirat Avatar asked Feb 07 '13 20:02

efirat


People also ask

Does LEFT join match NULL?

Left Join returns a null even when the match exists.

Can outer join have NULL values?

In SQL Full Outer Join, all rows from both the tables are included. If there are any unmatched rows, it shows NULL values for them.

How do I write a left join in HQL?

We can apply the Joins in Hibernate by using the HQL query or native SQL query. To make a join between the two tables, the two tables must be in a logical relationship. We can achieve the relationship between two tables by applying the parent table's primary key as a child table's foreign key.

IS LEFT join equal to left outer join?

There really is no difference between a LEFT JOIN and a LEFT OUTER JOIN. Both versions of the syntax will produce the exact same result in PL/SQL. Some people do recommend including outer in a LEFT JOIN clause so it's clear that you're creating an outer join, but that's entirely optional.


1 Answers

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
like image 93
JB Nizet Avatar answered Oct 04 '22 20:10

JB Nizet