Hi,
I've seen several postings regarding this topic, and tried several different hibernate settings (lazy true/false, outerjoin=true/false, etc etc), but I'm still at loss.
I have a class as follows:
/**
*
* @hibernate.class table="category"
* lazy="true"
*/
public class CategoryDTO implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private CategoryDTO parent;
private Set<CategoryDTO> children;
public CategoryDTO(){}
/**
* Unique identifier for the category.
*
* @hibernate.id
* generator-class="native"
* column="id"
* @hibernate.generator-param
* name="sequence"
* value="category_id_seq"
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
* Parent category for this category.
*
* @hibernate.many-to-one
* column="parentid"
* cascade="none"
* foreign-key="category_parent_fk"
*/
public CategoryDTO getParent() {
return parent;
}
public void setParent(CategoryDTO parent) {
this.parent = parent;
}
/**
* Child categories for this category
*
* @hibernate.set
* table="category"
* lazy="true"
* inverse="true"
* cascade="all"
* @hibernate.collection-one-to-many
* class="project.dto.CategoryDTO"
* @hibernate.collection-key
* column="parentid"
*
*/
public Set<CategoryDTO> getChildren() {
return children;
}
public void setChildren(Set<CategoryDTO> children) {
this.children = children;
}
}
Hibernate version:
3.1.2
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.otto.profiles.category.dto.CategoryDTO"
table="category"
lazy="true"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native">
<param name="sequence">category_id_seq</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-CategoryDTO.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<many-to-one
name="parent"
class="project.dto.CategoryDTO"
cascade="none"
outer-join="auto"
update="true"
insert="true"
foreign-key="category_parent_fk"
column="parentid"
/>
<set
name="children"
table="category"
lazy="true"
inverse="true"
cascade="all"
sort="unsorted"
>
<key
column="parentid"
>
</key>
<one-to-many
class="project.dto.CategoryDTO"
/>
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-CategoryDTO.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
// in a junit testcase
CategoryDTO dto = dao.findById(2L);
// should have loaded parent with id 1, but returns null
assertEquals(new Long(1), dto.getParent().getId());
// should have loaded children and printed 3 and 4 but set is empty
for (CategoryDTO child: dto.getChildren()) {
log.debug("Child:" + child.getId());
}
database contains following (loaded through db-unit)
<dataset>
<category
id="1"
nameleadingtextid="1"
/>
<category
id="2"
parentid="1"
nameleadingtextid="2"
/>
<category
id="3"
parentid="1"
nameleadingtextid="3"
/>
<category
id="4"
parentid="2"
nameleadingtextid="4"
/>
<category
id="5"
parentid="2"
nameleadingtextid="5"
/>
<category
id="6"
parentid="4"
nameleadingtextid="6"
/>
</dataset>
Full stack trace of any exception that occurs:
Name and version of the database you are using:
hsql db packaged with jboss4.0.3sp1
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
|