I have been trying to understand the paragraph on pp 261 in the book that states:
Quote:
HQL always ignores the mapping document eager fetch (outer join) setting
This is confusing, as when I try it, it seems like the complete opposite. With a really simple mapping file:
Code:
<class name="Category" table="categories">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="name" type="string" />
</class>
<class name="Item" table="items">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="name" type="string" />
<many-to-one name="category" column="category_id"
class="Category" outer-join="true"/>
</class>
<class name="Bid" table="bids">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="name" type="string" />
<many-to-one name="item" column="item_id"
class="Item" outer-join="true"/>
</class>
I get an exception if I try:
Code:
Session session = sessionFactory.openSession();
Bid bid = (Bid) session.createCriteria(Bid.class).uniqueResult();
System.out.println(bid.getName());
System.out.println(bid.getItem().getName());
session.close();
System.out.println(bid.getItem().getCategory().getName());
or if I replace with
Code:
Bid bid = (Bid) session.get(Bid.class,new Long(1));
If, however, I use an HQL query
Code:
Bid bid = (Bid) session.createQuery("from Bid").uniqueResult();
everything works fine. Removing the second System.out.println results in the org.hibernate.LazyInitializationException, but not if I remove the first. I am totally confused.
Jonas