Hello once again,
I have problems saving Parent along with Children.
* Parent .NET class:
Code:
int identity;
string name;
ISet<Child> children;
* Child .NET class:
Code:
int identity;
int parentId;
string name;
* Parent mapping file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SaveWithParent" assembly="SaveWithParent">
<class name="Parent" table="TEST_PARENT" dynamic-update="true" optimistic-lock="version">
<id name="Identity" column="PARENTID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SEQ__TEST_PARENT</param>
</generator>
</id>
<property name="Name" column="NAME" type="String" not-null="true"/>
<set name="Children" cascade="all">
<key column="PARENTID"/>
<one-to-many class="Child"/>
</set>
</class>
</hibernate-mapping>
* Child mapping file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SaveWithParent" assembly="SaveWithParent">
<class name="Child" table="TEST_CHILD" dynamic-update="true">
<id name="Identity" column="CHILDID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SEQ__TEST_CHILD</param>
</generator>
</id>
<property name="ParentId" column="PARENTID" type="Int32" not-null="true"/>
<property name="Name" column="NAME" type="String" not-null="true"/>
</class>
</hibernate-mapping>
* Program .NET code:
Code:
Configuration cfg = new Configuration();
cfg.AddAssembly( "SaveWithParent" );
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = null;
try
{
transaction = session.BeginTransaction();
// create and save child
Child child = new Child();
child.Name = "Slim Shary Jr. #1";
// create and save parent
Parent parent = new Parent();
parent.Name = "Slim Shady";
parent.Children = new HashedSet<Child>();
parent.Children.Add( child );
session.Save( parent );
// commit all of the changes to the DB and close the ISession
transaction.Commit();
}
catch ( System.Exception exc )
{
if ( transaction != null )
{
transaction.Rollback();
}
}
finally
{
session.Close();
}
* Database (Oracle 10g Release 2) statements:
Code:
NHibernate: select SEQ__TEST_PARENT.nextval from dual
NHibernate: select SEQ__TEST_CHILD.nextval from dual
NHibernate: INSERT INTO TEST_PARENT (NAME, PARENTID) VALUES (:p0, :p1); :p0 = 'Slim Shady', :p1 = '44'
NHibernate: INSERT INTO TEST_CHILD (PARENTID, NAME, CHILDID) VALUES (:p0, :p1, :p2); :p0 = '0', :p1 = 'Slim Shary Jr. #1', :p2 = '45'
Why parent id is not set for child creation statement?
Regards,
Mindaugas