Hi all
I am trying to have a many to many recursive relationship, and it is almost working but not quite.
the problem is that i can sucessfully add a parent category and its children categories.
However, when I try to retrieve a parent category and its relationship to the intermediate class (ParentChildCategories), it returns me an empty set...
is there something am missing here?
Hibernate version:
2.1.7
Mapping documents:
ParentChildCategories mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.terra.contact.model">
<class name="ParentToChildCategories" table="PARENT_CHILD_CATEGORIES">
<id name="id" type="long" column="PARENT_CHILD_CATEGORIES_PK" unsaved-value="null"
access="com.terra.contact.model.persistence.DirectSetAccessor">
<generator class="native" />
</id>
<!-- A versioned entity. -->
<version name="version" column="VERSION"
access="net.sf.hibernate.property.DirectPropertyAccessor" />
<!-- many end of a Affiliations relation -->
<many-to-one name="parentCategory" column="PARENT_CATEGORIES_PK" class="Categories" />
<!-- many end to User -->
<many-to-one name="childCategory" column="CHILD_CATEGORIES_PK" class="Categories" />
</class>
</hibernate-mapping>
This is an extract of the mapping for Categories
<!-- recursive relationship many to many
<set name="parentCategories" table="PARENT_CHILD_CATEGORIES" inverse="true" lazy="false" cascade="all">
<key column="CATEGORIES_PK" />
<one-to-many class="ParentToChildCategories" />
</set>
<set name="childCategories" table="PARENT_CHILD_CATEGORIES" inverse="true" lazy="false" cascade="all">
<key column="CATEGORIES_PK" />
<one-to-many class="ParentToChildCategories" />
</set>
<!-- end of recursive relationship many to many -->
Code between sessionFactory.openSession() and session.close():
and this is the code am trying to make work:
Code:
public Set getChildrenCategories() {
Set result = new HashSet();
Set childrenCategories = this.getChildCategories();
Iterator iterator = childrenCategories.iterator();
while(iterator.hasNext()) {
ParentToChildCategories parentToChildCategories = (ParentToChildCategories)iterator.next();
if (parentToChildCategories.getChildCategory()!=null)
result.add(parentToChildCategories.getChildCategory());
}
return result;
}
the line: this.getChildCategories returns me an empty set....
Full stack trace of any exception that occurs:Name and version of the database you are using:mysql
The generated SQL (show_sql=true):Debug level Hibernate log excerpt:Code: