Search code examples
jpahql

How do I return only the first object in an HQL cross join?


I have the following HQL

from 
    com.kable.web.allotment.model.Issue i 
    inner join fetch i.title 
    inner join fetch i.title.magazine 
    inner join fetch i.barcodes bcs 
    , Wholesaler w 
    LEFT join fetch w.localCurrencies c 
    inner join fetch w.location 
where 
    w.id = :wholesalerId
    and i.title.id = :titleid 
    and i.distributionStatus = :status 
    and (
            (
                i.distributionDate is null 
                and i.onSaleDate >= TRUNC(CURRENT_DATE)
            ) 
            or i.distributionDate >= TRUNC(CURRENT_DATE)
        ) 
    and bcs.type.id = w.location.id 
    and (bcs.localCurrency.id = c.localCurrencyType.id OR c.localCurrencyType.id IS NULL) 
    and i.onSaleDate BETWEEN COALESCE(c.effectiveDate, i.onSaleDate) and COALESCE(c.expirationDate, i.onSaleDate) 
order by 
    i.distributionDate
    , i.onSaleDate 

All of my previously written code is expecting to get a List<Issue> back, but with the code above, I am also getting the wholesaler and its joins. In my results, I only want Issue, Title, Magazine, and Barcodes. I am using hibernate version 4.2.18.Final. How do I only return the 1st object graph? I found something about CROSS JOIN ON but it is only for Hibernate 5 or later, and I can't switch because the project is quite large and Java dependencies.


Solution

  • You simply need to add an explicit SELECT i clause.

    As a side note, JOIN FETCH for Wholesaler associations doesn't make sense if it's not going to be present in the result anyway