-->
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.  [ 3 posts ] 
Author Message
 Post subject: tree data structure
PostPosted: Tue Sep 06, 2005 10:07 am 
Newbie

Joined: Fri Sep 02, 2005 12:47 pm
Posts: 14
Location: muenchen
I got troubles trying to hibernate tree like structure of data

I got class "node"

--------------------------------------
public class node {

private Long id;
private Long parent_id;

private node parent;
private String name;
private double number;
private List array = new ArrayList();

private String depth;
private String lineage;


public node(){
this.setParent(this);
}

public void addChildren(node ch){

ch.setParent(this);
this.getArray().add(ch);

}


------------------------

plus all setters and getters

Constructor is a bit strange because parent_id do not allows the null and one of the nodes have to be the root

and mapping file like this:

--------------------------------------


<class name="node" table="TREE">
<id name="id" column="ID">
<generator class="increment"/>
</id>

<property name="name"/>
<property name="depth"/>
<property name="lineage"/>

<!--CHILDREN-->
<list name="array"
inverse="true"
cascade="all-delete-orphan">

<key column="parent_id" not-null="true"/>
<list-index column="number"/>
<one-to-many class="node"/>
</list>

<!--PARENTS-->
<many-to-one name="parent_id"
class="node"
column="parent_id"
not-null="true" />

</class>

----------------------------------------------

When I try:

----------------------------------------------
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

node node1 = new node();
node node2 = new node();
node node3 = new node();

node1.setName("first");
node2.setName("second");
node3.setName("3rd");

node2.addChildren(node3);
node1.addChildren(node2);


session.save(node1);

tx.commit();
HibernateUtil.closeSession();
------------------------------------------------

I got:

16:00:29,979 INFO SchemaExport:173 - schema export complete
16:00:29,979 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:mysql://localhost/tutorial
16:00:29,979 INFO SessionFactoryImpl:379 - Checking 0 named queries
Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: data.object.node.parent_id
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:236)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at data.object.node.main(node.java:63)

------------------------------------------------------------

Do you have any suggestions what is the problem?


Sincerely,

Pawel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 11:19 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
The problem arises from the fact that all your nodes are nodes!! You have a not-null constraint on parent_id, so your root node must have a root node. Quick solutions:

1- remove the not-null constraint and monitor the values by java code
2- create a root_node_class and map it somehow without not-null constraints

Bye.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


Top
 Profile  
 
 Post subject: null
PostPosted: Tue Sep 06, 2005 11:34 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Well, Hibernate rightfully complains about parent_id being null because H does not-null check before persisting an object.

Is there a problem to use ‘assigned’ IDs – it is slightly less convenient but perhaps will help to avoid this problem.

As a side note: it looks like you are trying to let Hibernate to take care of entire tree structure. I do not think this is a good idea. I suggest looking at ‘nested set’ tree model from Joe Celko’s SQL for Smarties (and I definitely recommend the book) http://www.dbmsmag.com/9603d06.html

That model is really much more convenient to deal with in RDBMS, especially for hierarchical and roll-up queries.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


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