-->
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.  [ 5 posts ] 
Author Message
 Post subject: Help with tree node mapping
PostPosted: Tue Apr 25, 2006 10:59 am 
Beginner
Beginner

Joined: Tue Apr 25, 2006 10:46 am
Posts: 28
Hi,

Can anyone see anything wrong with the following mapping??

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class
name="org.testfuse.model.Node"
table="node"
>

<id
name="id"
column="id"
unsaved-value="null"
>


<generator class="native"></generator>

</id>
<property
name="name"
column="name"
not-null="true"
unique="true"
>

</property>

<list
name="children"
inverse="true"
cascade="all-delete-orphan"
>

<key column="parent_id">
</key>

<index column="position"/>

<one-to-many class="org.testfuse.model.Node"/>

</list>

<property name="position" column="position">

</property>

<many-to-one
name="parent"
class="org.testfuse.model.Node"
column="parent_id"
cascade="none"
>

</many-to-one>

</class>

</hibernate-mapping>


Because when I try to run the following unit test, it failed. But
I just can't see what's wrong. Everything else seem normal.
i.e.

public void testAddChildren() throws Exception {

Node node = dao.getNode(nodeId); // node not null
Node child1 = new Node();

// first child
child1.setName("first child of " + node.getName());
node.getChildren().add(child1);
child1.setParent(node);

dao.saveNode(node);

// test below FAILED!!!!!!!
// cascade="all-delete-orphan" line from mapping above
// should make child1.getId()!=null

// verify a primary key was assigned
assertNotNull("Expect non-null Id", child1.getId());

}

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

My DAO class uses Spring framework as below:

package org.testfuse.dao.hibernate;

import java.util.List;

import org.testfuse.model.Node;
import org.testfuse.dao.NodeDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class NodeDaoHibernate extends BaseDaoHibernate implements NodeDao {

/**
* @see org.testfuse.dao.NodeDao#getNodes(org.testfuse.model.Node)
*/
public List getNodes(final Node node) {
return getHibernateTemplate().find("from Node");
}

/**
* @see org.testfuse.dao.NodeDao#getNode(Long id)
*/
public Node getNode(final Long id) {
Node node = (Node) getHibernateTemplate().get(Node.class, id);
if (node == null) {
log.warn("uh oh, node with id '" + id + "' not found...");
throw new ObjectRetrievalFailureException(Node.class, id);
}

return node;
}

/**
* @see org.testfuse.dao.NodeDao#saveNode(Node node)
*/
public void saveNode(final Node node) {
getHibernateTemplate().saveOrUpdate(node);
}

/**
* @see org.testfuse.dao.NodeDao#removeNode(Long id)
*/
public void removeNode(final Long id) {
getHibernateTemplate().delete(getNode(id));
}
}

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 12:23 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I can. It's all left-aligned and proportially-spaced. It should have been wrapped in code tags.

There's also a bug in your mapping. The "children" list is inverse="true" but you're expecting it to save a child object. The whole point of inverse="true" is to prevent exactly that. Either remove the inverse="true", or save the child then put it in the children list.


Top
 Profile  
 
 Post subject: Thanks for reply but ...
PostPosted: Wed Apr 26, 2006 1:47 am 
Beginner
Beginner

Joined: Tue Apr 25, 2006 10:46 am
Posts: 28
Hi tenwit,

Thanks for reply but I have
cascade="all-delete-orphan", which
means when I save a node, Hibernate
will propagate change of the node's
children to database (I think),

so the test shud pass, unless my
knowledge of cascade is wrong.

Thanks,




[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 2:09 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Your knowledge of cascade is wrong. The inverse="true" mapping means that this side of the association will not create or delete objects on the other side. The cascade will correctly update those objects, and it would of course create and delete entries in the join table, if you were using a join table.

Other than taking my word for this, you can write a test. Oh wait, you already have. You're putting an item in a collection, saving the inverse end, and the object is not being created. Test is proving that inverse="true" is working.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Wed Apr 26, 2006 3:45 am 
Beginner
Beginner

Joined: Tue Apr 25, 2006 10:46 am
Posts: 28
Thanks Tenwit,

I got it. Problem solved. :)


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