Bidirectional Self-realtionship : Is it possible?
mandarjboss Jan 10, 2005 4:12 AMHi
I am having an Entity Bean "TestCategories". I want to have a bidirectional self-relationship for my entity bean.
So Ideally my entity bean will be something like ....
public abstract class QuestionCategoriesBean implements EntityBean {
public abstract void setQuestionCategoryID(long QuestionCategoryID);
public abstract long getQuestionCategoryID();
public abstract String getQuestionCategoryName();
public abstract void setQuestionCategoryName(String categoryName);
public abstract long getParentCategoryID();
public abstract void setParentCategoryID(long newParentCategoryID);
public abstract java.util.Collection getSubcategory();
public abstract void setSubcategory(java.util.Collection aSubcategory);
public abstract QuestionCategoriesLocal getParentcategory();
public abstract void setParentcategory(QuestionCategoriesLocal parentCategory);
}
In my ejb-jar.xml I have following declaration.
<ejb-relation>
<ejb-relation-name>QuestionCategories-QuestionCategories
</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>questioncategoriesParent</ejbrelationshiprole-name>
Many
<cascade-delete />
<relationship-role-source>
<ejb-name>QuestionCategories</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>parentcategory</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>questioncategories</ejb-relationship-role-name>
One
<relationship-role-source>
<ejb-name>QuestionCategories</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>subcategory</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
In jbosscmp-jdbc.xml I have following
<ejb-relation>
<ejb-relation-name>QuestionCategories-QuestionCategories</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>questioncategoriesParent</ejb-relationship-role-name>
<key-fields/>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>questioncategories</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>questionCategoryID</field-name>
<column-name>QuestionCategoryID</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
</ejb-relation>
I am pretty confused about having bidirectional self-relationship.
In database i have following data
ID Name Parent
1 Main
2 Second 1
3 Third 2
4 Fourth 2
In this case i have kept parent-id value for Main category blank.
Whenever i am executing code to retrieve all categories my code runs in an infinite loop.
I am not having any idea if such a kind of relationship is possible with CMR or not.
Can anybody tell me if such a kind of relationship is possible?