I have the following pieces of information in a parent-child association:
* the parent class
* the parent primary key
* the child collection property name
and I would like to load the child collection without loading the parent instance. Is this possible?
For example:
Code:
class Parent {
Long id;
Set childCollection;
Set otherCollection;
}
class Child {
Long id;
Parent parent;
//etc..
}
class Other {
Long id;
Parent parent;
//etc..
}
public Collection load(Class parent, Long id, String association) {
... how to implement?
}
With possible hibernate mappings:
Code:
<class name="Parent" table="T_PARENT" lazy="true" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" batch-size="1" select-before-update="false" optimistic-lock="version">
<id name="Id" column="Id" type="java.lang.Long">
<generator class="native"/>
</id>
<!-- bi-directional one-to-many association to Child -->
<set name="childCollection" table="T_Child" lazy="true" cascade="all" sort="unsorted" inverse="true" batch-size="1" outer-join="auto">
<key column="parentId"/>
<one-to-many class="Child"/>
</set>
<!-- bi-directional one-to-many association to Other -->
<set name="otherCollection" table="T_Other" lazy="true" cascade="all" sort="unsorted" inverse="true" batch-size="1" outer-join="auto">
<key column="parentId"/>
<one-to-many class="Other"/>
</set>
</class>
<class name="Child" table="T_CHILD" lazy="true" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" batch-size="1" select-before-update="false" optimistic-lock="version">
<id name="Id" column="Id" type="java.lang.Long">
<generator class="native"/>
</id>
<!-- bi-directional many-to-one association to Parent -->
<many-to-one name="parent" class="Parent" not-null="true" cascade="none" unique="false" outer-join="auto" update="true" insert="true">
<column name="parentIId"/>
</many-to-one>
</class>
<class name="Other" table="T_OTHER" lazy="true" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" batch-size="1" select-before-update="false" optimistic-lock="version">
<id name="Id" column="Id" type="java.lang.Long">
<generator class="native"/>
</id>
<!-- bi-directional many-to-one association to Parent -->
<many-to-one name="parent" class="Parent" not-null="true" cascade="none" unique="false" outer-join="auto" update="true" insert="true">
<column name="parentId"/>
</many-to-one>
</class>
I have a generic loader that gets Parent.class, "childCollection", and 1445 as the id of parent. But I don't know, for example, the property name of the child that references the parent (so I can't just go "select Child where child.parentid = 1445"). Is there a way I can load the children in one shot, without going through the extra step of loading the parent?
Thanks,
-Matt
Hibernate version: 2.1.6
Name and version of the database you are using: Oracle 9i