Hibernate version:2.1
Problem scenario:
We have a Parent object which has a one-to-many relation with the Child object.
In the Parent.hbm.xml file we mentioned that one-to-many relation with the Child object.
Child object has a Parent_ID parameter which maps to Parent's _Id. And in the Child table Parent_ID column is NOT NULL type.
In the Java Bean of Parent.java we have Child as List datamember. i.e something like
Code:
public class Parent
{
java.long.Long _Id;
java.lang.String _childName;
......
......
......
List _childList;
}
public class Child
{
java.long.Long _Id;
java.lang.String _name;
java.math.BigDecimal _parentId;
......
......
......
}
Now, I create a Parent object like
parent = new Parent();
parent.setName("parent");
Child child1 = new Child();
child1.setName( "child1");
List childList = new List();
childList.add( child1 );
parent.setChildList( childList );
parent.store(); // THIS GIVES ERROR Parent_Id cannot be NULL
THE ABOVE CODE DOESN'T WORK
Now, when I stoore the Parent object , then what I am expecting is Parent get's a unique Id when it gets inserted into the Parent table, and when it should cacadingly insert the children too with the corresponding Parent_Id.
But, this is not happening, it is throwing me an error saying that NULL cannot inserted into Parent_Id column in the CHILD table .
So, when I tried changing the code a little bit, like
Code:
parent = new Parent();
parent.setName("parent");
parent.store();
Long parentId = parent.getId();
Child child1 = new Child();
child1.setName( "child1");
child.setParentId( parentId );
List childList = new List();
childList.add( child1 );
parent.setChildList( childList );
parent.store(); //THIS STORES IT SUCCESSFULLY INTO CHILD TABLE
I dont understand why the first code snippet doesn't work. Any help is greatly appreciated.
This is impacting our project so, please respond ASAP.