Search code examples
nhibernatenhibernate-mappinghql

fetch="join" and hql


I'm quite confused about the fetch-attribute in a many-to-one mapping (class Order):

<many-to-one name="Product" column="ProductId" lazy="false" fetch="join" />

Now, if I write a hql-query like

from Order where Order.OrderId = x

Isn't Hibernate supposed to generate one SQL-Query, joining the Product? In my case, two queries happen and I'm not sure if the fetch-attribute within the mapping is ignored for some reason...


Solution

  • HQL does not respect fetch="join". You need to do it explicitly:

    from Order o
    join fetch o.Product
    o.OrderId = x
    

    (I might add lazy="false" is almost always a bad idea, but this will get you going)