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,
|