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.  [ 1 post ] 
Author Message
 Post subject: Problem with self-table reference
PostPosted: Wed Jun 01, 2005 4:30 am 
Newbie

Joined: Wed Jun 01, 2005 4:04 am
Posts: 1
I create a tree of categories. But when I try to take the root category exception rises. -1 in ParentId field means that this category is root of the tree.

net.sf.hibernate.UnresolvableObjectException: No row with the given identifier exists: -1, of class: org.binarus.domain.PreparationsCategory
at net.sf.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectException.java:38)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1954)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:69)
at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:204)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2205)
at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:315)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:305)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:911)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:931)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2117)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1991)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1953)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:69)
at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:204)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2205)
at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:315)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:305)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
at net.sf.hibernate.loader.SQLLoader.list(SQLLoader.java:92)
at net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3806)
at net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:52)


Here my mapping
<class name="org.binarus.domain.PreparationsCategory" table="PreparationsCategories">
<id name="id" column="ID" unsaved-value="-1">
<generator class="increment"/>
</id>
<property name="name" column="Name"/>
<set name="childCategories"
inverse="true"
cascade="all"
order-by="Name ASC">
<key column="ParentID"/>
<one-to-many class="org.binarus.domain.PreparationsCategory"/>
</set>
<many-to-one name="parentCategory"
class="org.binarus.domain.PreparationsCategory"
column="ParentID"
cascade="none" />
<set name="preparations"
inverse="true"
cascade="all"
order-by="Name ASC">
<key column="CategoryID"/>
<one-to-many class="org.binarus.domain.Preparation"/>
</set>
</class>

And here my class

public class PreparationsCategory {

private int id = -1;

private String name = "";

private String description = "";

private PreparationsCategory parentCategory;

private Set childCategories = new HashSet();

private Set preparations = new HashSet();

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public PreparationsCategory getParentCategory() {
return parentCategory;
}

public void setPerentCategory(PreparationsCategory parentCategory) {
this.parentCategory = parentCategory;
}

public Set getChildCategories() {
return childCategories;
}

public void setChildCategories(Set childCategories) {
this.childCategories = childCategories;
}

public void addChildCategory(PreparationsCategory category) {
if (category == null) {
throw new IllegalArgumentException("Null child category!");
}
if (category.getParentCategory() != null) {
category.getParentCategory().getChildCategories().remove(category);
}
childCategories.add(category);
category.setPerentCategory(this);
}

public void deleteChildCategory(PreparationsCategory category) {
if (category == null ) {
throw new IllegalArgumentException("Null child category!");
}
if (category.getParentCategory() != this) {
throw new IllegalArgumentException("Wrong child-parent relationship!");
}
childCategories.remove(category);
category.setPerentCategory(null);
}

public Set getPreparations() {
return preparations;
}

public void setPreparations(Set preparations) {
this.preparations = preparations;
}

public String toString() {
return name;
}


public void setParentCategory(PreparationsCategory parentCategory) {
this.parentCategory = parentCategory;
}
}

Code that I try to execute

StringBuilder query = new StringBuilder(
"SELECT {PreparationsCategories.*} FROM PreparationsCategories WHERE ");
query.append("ParentId=0");
query.append(" ORDER BY ID ASC, Name ASC");

categoriesList = session.createSQLQuery(query.toString(),
"PreparationsCategories", PreparationsCategory.class).list();

Tree has one root node with id=0 and ParentId=-1, all others nodes have ParentId 0 or another indeed exists node


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.