Hibernate version:2
I am creating a small application to learn O/R Mapping, but I failed. Hope somebody here can give me a hand.
here is the situation: there is a table named CATEGORY and its schema is CATEGORY (ID INT, NAME char(20), PARENT_ID INT). I created a POJO class named Category, which has four attributes
Code:
private int ID;
/**
* category name.
*/
private String name;
/**
* parent category
*/
private Category parent;
/**
* children category
*/
private Set children;
There is a many-to-one association from Category to Category itself, that is, a category can have many children categories. So here is the mapping file I created
Code:
<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.customizetree.hibernate.model">
<class
name="Category"
table="CATEGORY">
<id
name = "ID"
column = "ID"
type = "integer">
<generator class="increment"/>
</id>
<property
name = "name"
column = "NAME"
type = "string">
</property>
<many-to-one
name="parent"
column="PARENT_ID"
class="Category"
not-null="true"/>
<set
name="children"
inverse="true">
<key column="PARENT_ID"/>
<one-to-many class="Category"/>
</set>
</class>
</hibernate-mapping>
there is always a NullPointerException in the Category class