-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: loading a collection without loading the parent
PostPosted: Thu Mar 10, 2005 4:14 am 
Newbie

Joined: Fri Mar 04, 2005 2:13 am
Posts: 3
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 6:09 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
Use that query:
select c from Child as c where c.parent.id = 1445

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 12:54 pm 
Newbie

Joined: Fri Mar 04, 2005 2:13 am
Posts: 3
Perhaps I should have used a slighly different example. The actual situation is more like this:

Code:
class Parent {
  Long id;
  Set childCollection;
  Set otherCollection;
}

class Child {
  Long id;
  Parent parentOfChild;
  //etc..
}

class Other {
  Long id;
  Parent parentOfOther;
  //etc..
}

public Collection load(Class parent, Long id, String association) {
   ... how to implement?
}


So "parent" isn't always the name of the association. So all I have a handle to is the "many" end of the bi-di association. - I'd get a call like load(Parent.class, 1445, "childCollection"). Any way to load that?

Thanks,
-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 15, 2005 5:37 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
Simpliest way:
Use an upper-class where Child and Other extends that class , since Child and Other has ID and parent variables, move them up to that class, then
"parent" will always be the name of the association.

_________________
-developer


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.