-->
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.  [ 4 posts ] 
Author Message
 Post subject: MS SQL Server 2000/key generator problem
PostPosted: Tue Jun 21, 2005 10:43 pm 
Newbie

Joined: Tue Jun 21, 2005 10:20 pm
Posts: 3
I have been working with NHibernate 0.8.4 with some success, but the switch to letting NHibernate manage the key generation has failed for me.

Perhaps I have missed a configuration requirement for SQL Server. As a troubleshooting step I have tested my code with the generator class=assigned and everything works. However, when I go to native or identity, it appears that NHiberate is trying to insert the object without populating the primary key field thus causing an exception.

I have tried experimenting with the unsaved-value and explicit initialization, but with no success. Notice in the log that it does not attempt to insert a value for the primary key.

The success log is the assigned case.

I don't have the exception to include, but it is essentially a constraint violation because trying to add either a null field or a value that already exists for the primary key.

Any help would be appreciated as I am running out of troubleshooting ideas and need another set of eyes.

Let me review some of the details:

Persistence Class:

Code:
   public class Balance
   {
      private Int32 id;
      private DateTime busDate;
           //etc

      public Balance()
      {
      }

      public Int32 Id
      {
         get { return id; }
         set { id = value; }
      }
                //etc


Mapping:

Code:
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

    <class name="CohenSteers.AssetCoverage.Persistence.Balance, ACMReporter" table="BALANCE_TBL">
      <id name="Id" column="ID" type="Int32" unsaved-value="0">
         <generator class="native" />
      </id>
      <property name="BusinessDate" column="BUS_DATE" type="DateTime"/>
etc, etc


Failure Log:

2005-06-21 20:33:59,738 [2572] INFO NHibernate.Cfg.Configuration instantiating and configuring caches
2005-06-21 20:33:59,738 [2572] INFO NHibernate.Impl.SessionFactoryImpl building session factory
2005-06-21 20:33:59,831 [2572] INFO NHibernate.Impl.SessionFactoryObjectFactory no name configured
2005-06-21 20:34:03,785 [2572] INFO NHibernate.Impl.SessionImpl executing insertions
2005-06-21 20:34:03,800 [2572] INFO NHibernate.Impl.BatcherImpl Preparing INSERT INTO BALANCE_TBL (CD_DATE, BUS_DATE, FUND_ID, NAV_END_BAL, CRNCY_CD_LOC, CD_END_BAL, ACCOUNT, SUB_ACCOUNT, CRNCY_CD_FIN, CRNCY_CD_BASE, ACCOUNT_DESC) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10); select SCOPE_IDENTITY()


Success Log:

2005-06-21 20:41:32,959 [2760] INFO NHibernate.Impl.BatcherImpl Preparing INSERT INTO BALANCE_TBL (CD_DATE, BUS_DATE, FUND_ID, NAV_END_BAL, CRNCY_CD_LOC, CD_END_BAL, ACCOUNT, SUB_ACCOUNT, CRNCY_CD_FIN, CRNCY_CD_BASE, ACCOUNT_DESC, ID) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11)

CHW


Top
 Profile  
 
 Post subject: Exception - Autocommit settings?
PostPosted: Wed Jun 22, 2005 10:34 am 
Newbie

Joined: Tue Jun 21, 2005 10:20 pm
Posts: 3
Here is the exception that I receive. Could autocommit settings be causing a problem like this?

NHibernate.ADOException: Could not save object ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL in
o column 'ID', table 'cs.dbo.BALANCE_TBL'; column does not allow nulls. INSERT fails.
The statement has been terminated.
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean retu
nStream)
at System.Data.SqlClient.SqlCommand.ExecuteReader()


Top
 Profile  
 
 Post subject: Identity Property
PostPosted: Wed Jun 22, 2005 10:49 am 
Newbie

Joined: Tue Jun 21, 2005 10:20 pm
Posts: 3
I think I have resolved the situation. So the table must have the identity property set to true for the primary key field?


Top
 Profile  
 
 Post subject: Re: Identity Property
PostPosted: Thu Jun 23, 2005 9:04 am 
Regular
Regular

Joined: Mon May 16, 2005 2:15 pm
Posts: 59
chwilson wrote:
I think I have resolved the situation. So the table must have the identity property set to true for the primary key field?


Well yea. By setting it to native you are telling NHibernate that SQL will populate the pk. In order for SQL to do that the field must be an indentity field.

Bob


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