i thought that it would be possible to define which nested collections or nested persistent classes are loaded and how they are loaded at runtime by using the Criteria api?
But this seems not to be possible:
the DAO-layer:
Code:
public Concert loadConcert(long id, String[] fetchGroups) {
Criteria c = this.getSession().createCriteria(Concert.class)
.add(Restrictions.idEq(id));
if (fetchGroups != null) {
for (int i = 0; i < fetchGroups.length; ++i) {
c = c.setFetchMode(fetchGroups[i], FetchMode.SELECT);
}
}
return (Concert) c.uniqueResult();
}
the buisness-layer
Code:
public Concert getConcert(long id) {
Concert conc = this.concertDao.load(id, new String[] { "bands", "location" });
//session is closed, conc is detached
return conc;
}
the mapping file
Code:
<hibernate-mapping>
<class name="bandzine.domain.Concert" table="CONCERT">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" column="NAME"/>
<property name="fromDate" column="FROM_DATE"/>
<property name="toDate" column="TO_DATE"/>
<many-to-one name="location" column="LOCATION_ID" class="bandzine.domain.Location"/>
<set name="bands" table="CONCERT_BAND" lazy="false">
<key column="CONCERT_ID"/>
<many-to-many class="bandzine.domain.Band" column="BAND_ID"/>
</set>
</class>
<class name="bandzine.domain.Location" table="LOCATION">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" column="NAME"/>
<set name="concerts" inverse="true" lazy="false">
<key column="LOCATION_ID"/>
<one-to-many class="bandzine.domain.Concert"/>
</set>
</class>
<class name="bandzine.domain.Band" table="BAND">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" column="NAME"/>
<set name="concerts" inverse="true" table="CONCERT_BAND" lazy="false">
<key column="BAND_ID"/>
<many-to-many class="bandzine.domain.Concert" column="CONCERT_ID"/>
</set>
</class>
c.setFetchMode(fetchGroups[i], FetchMode.SELECT); does not fetch the nested Objects. So how can i define which nested objects are initialized before detaching
at runtime. it only seems to be possible to define it in the xml-mapping by setting the lazy property to lazy="false". But i don't want all nested objects to be loaded every time. i want to be able to create an individual fetch-plan at runtime! ORM like jdo support this, so why not hibernate??