-->
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.  [ 2 posts ] 
Author Message
 Post subject: self relationship never loading parent/children
PostPosted: Wed Feb 15, 2006 11:48 am 
Newbie

Joined: Wed Feb 15, 2006 11:31 am
Posts: 3
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:


Top
 Profile  
 
 Post subject: parent field can't be null in any row
PostPosted: Thu Feb 16, 2006 5:20 am 
Newbie

Joined: Wed Feb 15, 2006 11:31 am
Posts: 3
Hmmm,

The problem seems to be the root category, which doesn't have a parent category (so parentid is null in that row). When I set the parentid of the root category to point to itself getParent and getChildren worked.

Seems a bit odd, since there is no not-null constraint on the parentid on the category table. Why can't hibernate handle this scenario ? Or can it ? what am I missing.

One could argue in this case the problem isn't huge, but there are other self-relation scenarios where it would make perfekt sense where a foreign key field is not set in all cases.

rundis


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