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?