The default loading strategy is to use LAZY fetching for associated collections. And the recommended solution is to load the associated collections by overriding this default strategy at runtime in code. Both the Query and Criteria interfaces support this.
For example, if you have below entity association mapping:
public class XEntity {
...
private Set<YEntity> yentities;
}
}
then with Query interface, the associated yentities can be fetched along with the XEntity as this:
String xSelect = "select x from XEntity "
+ "x left join fetch x.yentities "
+ "where x.xName=:xname";
Query query = session.createQuery(xSelect);
and with Criteria interface, this can be done as this:
Criteria criteria = session.createCriteria(XEntity.class);
criteria.setFetchMode("yentities", FetchMode.JOIN);
Both of these two will fetch the associated yentities along with the XEntity, in a single select.
You can also use Hibernate#initialize to initialize the collection:
XEntity xentity = (XEntity) session.get(XEntity.class, id);
Hibernate.initialize(xentity.getYentities());
tx.commit();
session.close();
But its a good practice to fetch the complete required graph in the first place, using HQL or Criteria queries.