Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is uncommon to represent an association between objects by storing the id of the associated object, rather than the associated object itself. Storing the object is more expressive and type safe, and need not have a performance impact since hibernate features lazy loading proxies. </p> <p>But of course you can join things not mapped as associations in the mapping file. Quoting the <a href="http://docs.jboss.org/hibernate/core/3.5/reference/en/html/queryhql.html#queryhql-from" rel="noreferrer">hibernate reference manual</a>:</p> <blockquote> <p>Multiple classes can appear, resulting in a cartesian product or "cross" join.</p> <pre><code>from Formula, Parameter from Formula as form, Parameter as param </code></pre> </blockquote> <p><a href="http://docs.jboss.org/hibernate/core/3.5/reference/en/html/queryhql.html#queryhql-select" rel="noreferrer">and</a></p> <blockquote> <p>Queries can return multiple objects and/or properties as an array of type Object[]:</p> <pre><code>select mother, offspr, mate.name from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr </code></pre> <p>Or as a List:</p> <pre><code>select new list(mother, offspr, mate.name) from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr </code></pre> <p>Or - assuming that the class Family has an appropriate constructor - as an actual typesafe Java object:</p> <pre><code>select new Family(mother, mate, offspr) from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr </code></pre> <p>You can assign aliases to selected expressions using as:</p> <pre><code>select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n from Cat cat </code></pre> <p>This is most useful when used together with select new map:</p> <pre><code>select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n ) from Cat cat </code></pre> <p>This query returns a Map from aliases to selected values.</p> </blockquote> <p>Combining this with a simple where clause, we get:</p> <pre><code>select product, item from Product product, Item item where item.product_id = product.product_id </code></pre> <p><strong>Edit</strong>: I missed that you want an <em>outer</em> join. In that case I don't know a way other than mapping the association so you can do an ordinary join on it. Note that this does not necessitate a particular form of the query result, i.e. you can still do:</p> <pre><code>select product, item from Item item left join item.product as product </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload