-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Problem: additional column in insert statement
PostPosted: Tue Dec 04, 2007 2:03 pm 
Beginner
Beginner

Joined: Tue Nov 27, 2007 1:25 pm
Posts: 24
* 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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 2:12 pm 
Beginner
Beginner

Joined: Tue Nov 27, 2007 1:25 pm
Posts: 24
Well.. it worked when I added insert="false" to <many-to-one> relation, but is this OK?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 2:56 pm 
Newbie

Joined: Wed Jun 20, 2007 8:22 pm
Posts: 2
You don't need to map the PARENTID property in your child mapping file. Hibernate will figure that out for you by just using the many-to-one entry.

So, remove this from the child mapping...
Code:
<property name="ParentId" column="PARENTID" type="Int32" not-null="false"/>


...and the extra PARENTID column insert should go away.

FYI, I've found that your life is much easier if you just let hibernate insert your primary key via increment or sequence instead of doing it via a trigger....of course, sometimes you can't avoid.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.