-->
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: Many to one relationship to itself (Problem with HQL Query)
PostPosted: Mon May 08, 2006 1:22 am 
Newbie

Joined: Mon May 08, 2006 1:07 am
Posts: 2
What is wrong with my code, Help me.

Below is my java class, hibernate mapping in hbm.xml. and HQL Query
I need to retrive the top level categories from table, means whose
parentId = 0

Tabel data
id name shortName parentId
1 A a 0
2 B b 0
3 A1 a1 1
4 C c 0
---------------------------------------

package symbio.jobs.businessObjects;

import java.io.Serializable;

import stellar.commons.DropDownItem;
import stellar.commons.businessEntities.BusinessEntity;

/** @author Hibernate CodeGenerator */
public class Category extends BusinessEntity implements Serializable, DropDownItem {

/**
*
*/
private static final long serialVersionUID = 1L;

/** persistent field */
private String name;

/** persistent field */
private String shortName;

/** persistent field */
private Category parentCategory;

/**
* @return Returns the parentCategory.
*/
public Category getParentCategory() {
return parentCategory;
}

/**
* @param parentCategory The parentCategory to set.
*/
public void setParentCategory(Category parentCategory) {
this.parentCategory = parentCategory;
}

/** full constructor */
public Category(String name, String shortName) {
this.name = name;
this.shortName = shortName;
}

/** default constructor */
public Category() {
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getShortName() {
return this.shortName;
}

public void setShortName(String shortName) {
this.shortName = shortName;
}

public String toString() {
return name;
}


public Object getDisplayString() {
return name;
}

public Object getKey() {
return getId();
}


}

<class name="symbio.jobs.businessObjects.Category" table="categories" >
<id name="id"><generator class="identity"/></id>
<property name="name" type="string" not-null="true" unique="true" />
<property name="shortName" type="string" not-null="true" unique="true" />
<many-to-one name="parentCategory" column="parentId" class="symbio.jobs.businessObjects.Category" />
</class>

(HQL Query)

FROM Category category where category.parentCategory = 0


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 8:45 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
You don't say exactly what your problem is but it might well stem from the fact that Hibernate doesn't know that a parentId of 0 means that it has no parent. It would be better to have nulls for those items in the column but if not you could set not-found to ignore on the many-to-one and query using something like:-

Code:
FROM Category category where category.parentCategory.id = 0


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.