|
Hi,
I want to save a child record from Child side of the relationship. The reason to save from Child side of relationship is to avoid fetching of all child records. For me, saving from Child side means:
session.save(child);
But while saving i get following error:
rg.hibernate.PropertyValueException: not-null property references a null or transient value: Child._childrenBackref at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
I have tired in all possbile ways but all in vain. I also referred a lot of posts but that too did not help.
The datastructure used for child records is List. My Parent and Child hbms look as given below:
<class name="Parent" table="PARENT"> <id name="id" type="int" column="id" > <generator class="assigned"/> </id> <property name="name"> <column name="name" /> </property> <list name="children" lazy="true"> <key column="parentid" not-null="true" /> <list-index column = "parent_child" /> <one-to-many class="Child"/> </list>
<hibernate-mapping> <class name="Child" table="CHILD"> <id name="id" type="int" column="ID" > <generator class="assigned"/> </id> <property name="name"> <column name="NAME" /> </property> <many-to-one name="parentId" column="parentid" class="Parent" insert="false" update="false" not-null="true"/> </class> </hibernate-mapping>
The code used to save the child record is:
Parent parent = (Parent)session.load(Parent.class, 2); Child child = new Child(); child.setId(7); child.setName("c7"); child.setParentId(parent); session.save(child);
Though above code works when i also set child in parent object
as given below:
parent.getChildren().add(child)
But that is what i want to avoid as it fetches all children.
Anybody please help !
Thanks in advance !
|