im pretty new with Hibernate..
I have a table named accountHeadGroups
Code:
id parentId code name
1 0 a abc
2 0 b bmn
3 1 c fffff
4 3 d gggg
(abc(root) -> fffff (child) -> gggg (child))
if the parentId is zero then it is the root element.
Code:
<hibernate-mapping>
<class name="pojo.AccountHeadGroup" table="accountHeadGroups">
<id name="id" type="int" column="id" >
<generator class="increment"/>
</id>
<set name="children" inverse="true" cascade="all-delete-orphan" lazy="true" order-by="id" >
<key column="parentId" not-null="true" />
<one-to-many class="pojo.AccountHeadGroup" />
</set>
<many-to-one name="parent"
column="parentId"
class="pojo.AccountHeadGroup"
not-null="true"
insert="true"
update="true"
lazy="false"
not-found="ignore"
/>
<property name="code">
<column name="code"/>
</property>
<property name="name">
<column name="name"/>
</property>
</class>
</hibernate-mapping>
Now, if I want to add a new AccountHeadGroup then
AccountHeadGroup ahg = new AccountHeadGroup();
ahg.setParent(someParent);
ahg.setCode("c");
ahg.setName("name");
session.save(ahg); and it'll work fine
but when I want to add/edit an AccountHeadGroup myGroup, whose parentId is 0 (root element), then myGroup's parent would be null.. and if I try to saveOrUpdate myGroup, it will throw org.hibernate.PropertyValueException: not-null property references a null or transient value: pojo.ItemGroup.parent
any advice????