-->
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.  [ 3 posts ] 
Author Message
 Post subject: retrieving a collection from database by ID of FK
PostPosted: Wed Jan 18, 2006 6:43 pm 
Newbie

Joined: Wed Jan 18, 2006 6:24 pm
Posts: 2
I've been trying different ways to do this, but just can't seem to figure it out. I have two different objects, represented in this way ...

Parent - FoodGrpLst
Child - FoodSubGrpLst

The mapping files for these classes is as follows..

for FoodGrpLst...
<class name="webapp.lists.FoodGrpLst" table="FOOD_GROUPS" mutable="false">
<id name="foodGroupID" column="FOOD_GROUP_ID">
<generator class="native"/>
</id>
<property name="foodGroupNameE" column="FOOD_GROUP_EN_NAME"/>
<property name="foodGroupNameF" column="FOOD_GROUP_FR_NAME"/>
</class>


for FoodSubGrpLst...
<class name="webapp.lists.FoodSubGrpLst" table="FOOD_SUBGROUPS" mutable="false">
<id name="foodSubgroupID" column="FOOD_SUBGROUP_ID">
<generator class="native"/>
</id>

<property name="foodSubgroupNameE" column="FOOD_SUBGROUP_EN_NAME"/>
<property name="foodSubgroupNameF" column="FOOD_SUBGROUP_FR_NAME"/>

<many-to-one name="foodGroup"
class="webapp.lists.FoodGrpLst"
column="FOOD_GROUP_ID" cascade="none"/>
</class>


The class parameter declarations for FoodSubGrpLst starts off like this ..

public class FoodSubGrpLst {

private Long foodSubgroupID;
private String foodSubgroupNameE;
private String foodSubgroupNameF;

private FoodGrpLst foodGroup = new FoodGrpLst();

private Long foodGroupID;
.
.
.
}


In my DAO for FoodSubGrp i would like to retrieve a collection of FoodSubGrp's based on a FoodGrp ID.

The method for this looks like this ...

public Collection getFoodSubGroupsbyFoodGroupID(Long foodGroupID, boolean lock) throws DBException {

Session session = HibernateUtil.getSession();
FoodSubGrpLst foodSubgroup = null;
Collection foodSubgroupList = null;

try {
Criteria crit = HibernateUtil.getSession().createCriteria FoodSubGrpLst.class);
crit.add(Expression.eq("foodGroup", foodGroupID));
foodSubgroupList = crit.list();
}
catch (HibernateException ex) {
}
return foodSubgroupList;
}


On the line crit.add(Expression.eq("foodGroup", foodGroupID)); is where the problem is. "foodGroup" is the parameter in my mapping file that maps to the foreign key FOOD_GROUP_ID, however in my FoodSubGrpLst object it is represented as an object ...so I am thinking this is causing some problems. Anyone know a correct way to do what I'm trying to do?

Any help would be appreciated,
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 11:17 pm 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
i think ur senario is a parent-child relationship for foodgrp-foodsubgrp .
in that case , the mapping for foodsubgrp in foodgrp may be a collection .
so that you can add a sub group like foodgrp.add(foodsubgrp)
if so you can load the foodgrp using the id (session.load()) and simply use
foodgrp.getFoodSubGrps() to retrieve those subgroups. i think it will be a better way . i hope this will solve ur problem :)

_________________
sHeRiN
thanks for your ratings ...... :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 4:47 pm 
Newbie

Joined: Wed Jan 18, 2006 6:24 pm
Posts: 2
THanks zzyborg. I used your suggestion and made the changes in my mapping file. I then didn't need to write a method to retrieve the foodSubGroups in my DAO at all. :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.