Hello,
I have a question about Lazy Parent-Child relationships. I've read chapter 8 several times and searched the forum but I come up empty. This question applies to Hibernate 2.0.
We have a series of relationships: Category -> SubCategory -> SubSubCategory -> Part
We have them setup with lazy initialization between the sets.
Our DAO has methods to return Lists of parents and children.
e.g.
Collection findCategories()
Collection findSubCategories(int categoryId)
Collection findSubSubCategories(int subcategoryId)
etc.
These methods are used to populate a series of dropdown boxes in a web application.
For the findSubCategories() method we're implementing a simple:
session.query(" from SubCategory as subcategory ");
We're not loading all of the subsubcategories for obvious reasons. This query returns the foreign keys to Category as it should.
I'm curious about the findSubSubCategories(int subcategoryId) method. Should we use:
SubCategory subcat = (SubCategory)session.load(subcategoryId, SubCategory.class);
return subcat.getSubSubCategories(); // one to many
(This code first loads the subcategories and associated category fk using a left outer join and second loads the subsubcategories)
or is it better to issue a query for this:
session.query(" from Subcategory as subcategory where subcategory.category.id = ? ");
(The queries load the subsubcategories first, and then loads the subcategories and associated category fk using a left outer join.)
Is this just a matter of preference? Is there anyway of eliminating the query to the subcategory, or is this needed to keep the relationships intact.
Each request will be using a new session, since they come through as new requests from the web-client.
Thank you.
P.S. I'm anxiously awaiting the hibernate book.
_________________ - Brian
|