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.  [ 5 posts ] 
Author Message
 Post subject: Issue with inserting not sure if it code or my relation map?
PostPosted: Mon Oct 27, 2008 11:39 am 
Newbie

Joined: Mon Oct 27, 2008 11:37 am
Posts: 4
Hi,



I am using nhibernate to insert data into a table but when it comes to relation tables and inserting into a child table I always get the error that the foreign key is always null. I am pretty sure I am doing it right and I know the id is not null when it passed over.



My parent map look like this.
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="eImage2" assembly="eImage2" >
  <class name="nUsers" table="Users" schema="dbo">
    <id name="Id" column="UserID" type="Int32">
      <generator class="identity"/>
    </id>
    <!--  one-to-one mapping: Types-->
    <many-to-one name="Type" class="nTypes" column="TypeID" cascade="none" />
    <!-- A cat has to have a name, but it shouldn' be too long. -->

    <property name="Username" column="Username" type="string"/>
    <property name="Password" column="Password" type="string"/>
    <property name="Forename" column="Forename" type="string"/>
    <property name="Surname" column="Surname" type="string"/>
    <property name="TypeId" column="TypeID" type="Int32"/>
    <property name="Created" column="Created" type="Date"/>

    <bag name="Files" table="Files" cascade="all" lazy="false">
      <key column="UserID"/>
      <one-to-many class="nFiles" />
    </bag>

  </class>
</hibernate-mapping>

My child map looks like this:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="eImage2" assembly="eImage2" >
  <class name="nFiles" table="Files" schema="dbo">
    <id name="Id" column="FileID" type="Int32">
      <generator class="identity"/>
    </id>
    <!--  one-to-one mapping: Customer -->
    <many-to-one name="User" class="nUsers" column="UserID" cascade="none"/>
    <!-- A cat has to have a name, but it shouldn' be too long. -->

    <property name="FileName" column="FileName" type="string"/>
    <property name="FileType" column="FileType" type="string"/>
    <property name="FileSize" column="FileSize" type="string"/>
    <property name="IPAddress" column="IPAddress" type="string"/>
    <property name="Approved" column="Approved" type="boolean"/>
  </class>

</hibernate-mapping>



The code that I call looks like this:
Code:
public void dbFileInsert(int id, string fileName, int fileSize, string fileType, string ip, out int status)
        {
            // Get the Session from NHibernateHttpModule
            int result = 0;
            try
            {
                ISession session = NHibernateControl.GetCurrentSession();

                ITransaction tx = session.BeginTransaction();

                nUsers u2 = session.Get(id);
                nFiles f2 = new nFiles();
                f2.FileName = fileName;
                f2.FileType = fileType;
                f2.FileSize = fileSize;
                f2.IPAddress = ip;
                f2.Approved = false;
                u2.Files.Add(f2);
                session.Save(u2);
                session.Flush();
                tx.Commit();


            }
            catch (HibernateException ex)
            {
                NHibernateControl.CloseSession();
                setMsg(ex.Message);
                result = -1;
            }
            catch (Exception ex)
            {
                NHibernateControl.CloseSession();
                setMsg(ex.InnerException.Message);
                result = -1;
            }
            NHibernateControl.CloseSession();

            status = result;
        }


The error I keep recieving is:

Error : Cannot insert the value NULL into column 'UserID', table 'C:\DOCUMENTS AND SETTINGS\ADC060\MY DOCUMENTS\VISUAL STUDIO 2008\PROJECTS\EIMAGE2\EIMAGE2\APP_DATA\EIMAGE2.MDF.dbo.Files'; column does not allow nulls. INSERT fails. The statement has been terminated.



I am guessing it is either to do with my map or to do with the way I am trying to insert the new data. My tables do not allow nulls for the primary or foreign key in either map.



Thanks inadvance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 3:10 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
Your foreign key is null because you have never set it.

You have to do
Code:
f2.User = u2;

yourself, NHibernate doesn't do that for you.

Oh, and you should mark your bag as inverse="true", otherwise you will have further problems with foreign keys.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 5:25 am 
Newbie

Joined: Mon Oct 27, 2008 11:37 am
Posts: 4
Thanks for the reply.

I slowly figured out that issue myself last night, but thats for the reply.

I am now getting a new error.

The error just says

Invalid index 7 for this SqlParameterCollection with Count=7

My code now looks like Parent xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="eImage2" assembly="eImage2" >
  <class name="nUsers" table="Users" schema="dbo">
    <id name="Id" column="UserID" type="Int32">
      <generator class="identity"/>
    </id>
    <!--  one-to-one mapping: Types-->
    <many-to-one name="Type" class="nTypes" column="TypeID" not-null="true"/>
    <!-- A cat has to have a name, but it shouldn' be too long. -->

    <property name="Username" column="Username" type="string"/>
    <property name="Password" column="Password" type="string"/>
    <property name="Forename" column="Forename" type="string"/>
    <property name="Surname" column="Surname" type="string"/>
    <property name="TypeId" column="TypeID" type="Int32"/>
    <property name="Created" column="Created" type="Date"/>
   
    <bag name="Files" inverse="true" cascade="all" lazy="false">
      <key column="UserID"/>
      <one-to-many class="nFiles" />
    </bag>
   

  </class>
</hibernate-mapping>

child xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="eImage2" assembly="eImage2" >
  <class name="nFiles" table="Files" schema="dbo">
    <id name="Id" column="FileID" type="Int32">
      <generator class="identity"/>
    </id>
    <!--  one-to-one mapping: Customer -->
    <many-to-one name="User" class="nUsers" cascade="all" column="UserID" not-null="true"/>
    <!-- A cat has to have a name, but it shouldn' be too long. -->

    <property name="FileName" column="FileName" type="string"/>
    <property name="FileType" column="FileType" type="string"/>
    <property name="FileSize" column="FileSize" type="string"/>
    <property name="IPAddress" column="IPAddress" type="string"/>
    <property name="Approved" column="Approved" type="boolean"/>
  </class>

</hibernate-mapping>


.net code.

Code:
public void dbFileInsert(int id, string fileName, int fileSize, string fileType, string ip, out int status)
        {
            // Get the Session from NHibernateHttpModule
            int result = 0;
            nUsers u2 = new nUsers();
            nFiles f2 = new nFiles();
            try
            {
                ISession session = NHibernateControl.GetCurrentSession();

                ITransaction tx = session.BeginTransaction();
                u2 = (nUsers)session.Load(typeof(nUsers), id);

                f2.User = u2;
                f2.FileName = fileName;
                f2.FileType = fileType;
                f2.FileSize = fileSize;
                f2.IPAddress = ip;
                f2.Approved = false;
                u2.Files.Add(f2);
                session.Save(u2);
                session.Flush();
                tx.Commit();


            }
            catch (HibernateException ex)
            {
                NHibernateControl.CloseSession();
                setMsg(ex.Message);
                result = -1;
            }
            catch (Exception ex)
            {
                NHibernateControl.CloseSession();
                setMsg(ex.Message);
                result = -1;
            }
            NHibernateControl.CloseSession();

            status = result;
        }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 6:21 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
Could you please post the generated SQL statement which fails?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2008 5:16 am 
Newbie

Joined: Mon Oct 27, 2008 11:37 am
Posts: 4
I figured it out it was to do with my createdOn field in the parent map, it was to do with the datatypes not matching between the server and the map type, they are suppose to be date.

The map has datetime the sql server has date which seems to conflict I have removed it for the time being but need to figure out correct data types in the long run.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.