-->
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 representation in Hibernate
PostPosted: Sun Jan 08, 2006 3:50 pm 
Newbie

Joined: Sun Oct 17, 2004 4:43 am
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Hi

Need some help with trees representation

My mapping:

<class name="VoipDeviceRelationsTreeImpl" table="TPG_VOIP_DEVICE_RELATIONS_TREE" lazy="false">
<cache usage="read-write"/>

<!-- Primary key -->
<id name="id" column="NODE_ID" type="int" unsaved-value="-1" >
<generator class="foreign" >
<param name="property">node</param>
</generator>
</id>

<one-to-one name="node" class="NodeImpl" constrained="true" />

<many-to-one lazy="false"
name="parent" unique-key="parent_name_uk1"
class="VoipDeviceRelationsTreeImpl"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="PARENT_ID"
not-null="false"

/>

<set name="children" inverse="true" cascade="all-delete-orphan" >
<cache usage="read-write"/>
<key column="PARENT_ID" not-null="true" />
<one-to-many class="VoipDeviceRelationsTreeImpl" />
</set>

</class>


Note: the mapping above represents nodes in other table, mapped via NodeImpl class.

Code snapshot:

protected void createTopology(Session session) {
TopologyHibernateDao topologyDao = new TopologyHibernateDao();
topologyDao.setSessionFactory(getHibernateTest().getSessionFactory());

Node node1 = createFirstTopologyNode(topologyDao);
Node node2 = createSecondTopologyNode(topologyDao);

Node node3 = topologyDao.createNode();
VoIPDevice voIPDevice= node3.getOrCreateVoipDevice(1,"http://ggg");
voIPDevice.getOrCreateProperty(voIPDevice.getDeviceId(),"xxx","yyy");
voIPDevice.getOrCreateProperty(voIPDevice.getDeviceId(),"ttt","mmm");
session.save(node1);
session.flush();
session.save(node2);
session.flush();
session.save(node3);
session.flush();
VoipDeviceRelationsTree voipDeviceRelationsTree1 = new VoipDeviceRelationsTreeImpl(node1, null);
session.save(voipDeviceRelationsTree1);
session.flush();
VoipDeviceRelationsTree voipDeviceRelationsTree2 = new VoipDeviceRelationsTreeImpl(node2, voipDeviceRelationsTree1);
session.save(voipDeviceRelationsTree2);
session.flush();
VoipDeviceRelationsTree voipDeviceRelationsTree3 = new VoipDeviceRelationsTreeImpl(node3, voipDeviceRelationsTree2);
session.save(voipDeviceRelationsTree3);
session.flush();

session.getSessionFactory().evict(VoipDeviceRelationsTreeImpl.class);
session.disconnect();
List list2;
try
{
list2 = session.createQuery("from VoipDeviceRelationsTreeImpl as treeItem where treeItem.id = 1").list();
}
catch(Exception ex)
{

}
session.reconnect();

List list1 = session.createQuery("from VoipDeviceRelationsTreeImpl as treeItem where treeItem.id = 1").list();
list2 = session.createQuery("from VoipDeviceRelationsTreeImpl as treeItem where treeItem.node.id = ?").setInteger(0, node2.getNodeId()).list();
List list3 = session.createQuery("from VoipDeviceRelationsTreeImpl as treeItem where treeItem.node.id = ?").setInteger(0, node3.getNodeId()).list();
}


provblem:

In all cases children property which is a Set, gets null, though node3 suppose to be the chilled of node2 and node2 is the chilled of node1 (node 1 is the root since it's parent is null)

What may be wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 9:19 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello,

it is not clear form your post, if you proper set the relations.
e.g. if you do the following:
If you missed the setParent(xy), your relation will not be saved.

Code:
   TreeItem sub1 = new TreeItem();
   sub1.setName("sub1");
   item.getChildren().add(sub1);
   sub1.setParent(item);
   session.save(item);
   session.save(sub1);


Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 9:26 am 
Newbie

Joined: Sun Oct 17, 2004 4:43 am
Posts: 4
Hi Sebastian
and 10x for the reply
I actually doing the setParent (for each new tree -node) but not the add to children, I expect the child to be resolved from the mapping

is it to much to ask?


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.