* Intro:
Child --> Parent;
Child.ParentId references Parent.ParentId;
Child.ParentId is not meant to be inserted "from code" - trigger on database server handles this column value.
* Situation 1:
Child doesn't have a <many-to-one> reference to Parent in it's mapping file;
Create new Child, specify ParentId = null in code (ParentId is Int32, nullable), Save, you get insert statement where ParentId = '' (null); trigger sets ParentId to some value and everything is OK;
Code:
NHibernate: select SEQ__TEST_CHILD.nextval from dual
NHibernate: INSERT INTO TEST_CHILD (PARENTID, NAME, CHILDID) VALUES (:p0, :p1, :p2); :p0 = '', :p1 = 'Some Jack Ripper', :p2 = '64'
* Situation 2:
Child has a <many-to-one> reference to Parent in it's mapping file;
Same situation, BUT insert statement is generated with two ParentId columns..
Code:
NHibernate: select SEQ__TEST_CHILD.nextval from dual
NHibernate: INSERT INTO TEST_CHILD (PARENTID, NAME, PARENTID, CHILDID) VALUES (:p0, :p1, :p2, :p3); :p0 = '', :p1 = 'Some Jack Ripper', :p2 = '', :p3 = '63'
* 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="false"/>
<property name="Name" column="NAME" type="String" not-null="true"/>
<!--<many-to-one class="Parent" name="Parent" column="PARENTID" fetch="join" lazy="false" not-null="false"/>-->
</class>
</hibernate-mapping>
* 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>
<timestamp column="RECORDVERSION" name="RecordVersion" generated="never"/>
<property name="Name" column="NAME" type="String" not-null="true"/>
</class>
</hibernate-mapping>
* Tech specifications:
Oracle 10g Release 2
NHibernate 1.2
How to "remove" that additional column from insert statement in Situation 2? I understand that NHibernate adds column to insert statement if that column is mentioned in <many-to-one> mapping..?
Regards,
Mindaugas