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.  [ 8 posts ] 
Author Message
 Post subject: New to NHibernate... with questions :)
PostPosted: Tue Jan 02, 2007 12:07 am 
Newbie

Joined: Mon Jan 01, 2007 11:33 pm
Posts: 10
As stated I'm new to NHibernate. I've gotten the Cat example to work, but when I tried to get a simple class working with my actual data I came across a problem that I'm hoping someone can help me with.

Keep in mind that I'm not an SQL Server expert, being more familiar with MySQL.



I'm using NHibernate-1.2.0Beta2 with MS SQLServer 2000, and the site is using ASP.Net 2.0 which I'm editing with Web Developer Express.


In a nutshell I've defined my schema, placed the assemblies, etc, but it keeps trying to insert a null for the primary key.


Here's the relevant bits:

schema
Code:
<class name="PhoneNumber" table="mega_phone_numbers">

   
   
    <id name="id">
      <column name="id" sql-type="int" not-null="true" unique="true"/>
      <generator class="identity"/>
    </id>



POCO class
Code:
        public virtual int id
        {
            get { return _id; }
            set { _id = value; }
        }



The primary key is setup as an int identity with an increment of 1.


Whenever I attempt to insert into the table I get the following error:

Code:
Cannot insert the value NULL into column 'id', table 'mega_test_db.dbo.mega_phone_numbers'; column does not allow nulls. INSERT fails.


----------------------------

Also note the sql-type. I tried to use type="Int32" but it kept giving me an error about it not being supported.


I've been banging my head against this problem for several hours now so any advice would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 8:24 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

Don't know if this will help, but this is an example of an id-declaration in our mapping files:
Code:
<id name="Id" type="Int32" unsaved-value="0" column="app_profile_id">
      <generator class="native" />
</id>


Please rate this post if it helped.
X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 9:55 am 
Newbie

Joined: Mon Jan 01, 2007 11:33 pm
Posts: 10
Thank you for the reply xasp.

I can see from your example part of my misunderstanding. The type attribute is for the id/property, while the sql-type is for the column. I feel a bit foolish, but I was so aggravated yesterday I don't think I was paying attention the way I should have been.


Unfortunately I'm still getting the exact same error. Here's my current xml file:

Code:
    <id name="id" column="id" type="Int32">
      <generator class="identity"/>
    </id>


I've tried both native and identity generators with the same error.

In the interest of being complete I'll list the assemblies that I'm referencing:

Code:
Castle.DynamicProxy.dll
Iesi.Collections.dll
log4net.dll
NHibernate.dll


Is it possible that I'm not referencing a needed assembly? I'll look around, but if someone could nudge me in the right direction I'd really appreciate it.

PS
I went ahead and rated you even though it didn't solve the problem since it helped me understand the xml files better.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 10:07 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
In the NHibernate configuration file, are you using the MSQL2000 dialect ?
Code:
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>


X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 10:18 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
By the way, what statement are you using for persisting your object to the datastore: session.Save(), Update() or SaveOrUpdate() ?
As you're not using the unsaved-value attribute, maybe NHibernate is trying to execute an Update instead of an Insert (although the message is quite explicitely saying it's an insert that is failing).

Could you maybe send some more code along ?

X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 10:58 am 
Newbie

Joined: Mon Jan 01, 2007 11:33 pm
Posts: 10
I apologize I should have updated the thread.

The problem was user error. I had made a few changes to the table involving the primary key, relationships, et al, and I forgot to set the new primary key column to identity...

I'm traditionally a linux guy so I'm picking up C#, ASP.Net, SQL Server, & NHibernate all at the same time. At least that's my excuse as I can't believe I actually missed that (it just clicked in my head this morning).

Nothing like a good old fashioned PEBKAC error. I promise you I'll never make that mistake again, although I'm now officially happy with NHibernate :)

I do have 1 more question. Is there a default-value attribute for NHibernate? or do I need to reflect that in the code itself?


Thanks for all the help xasp, you've definately helped me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 11:19 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
I'm glad you found the error, because I was also at a loss about the possible causes. ;-)

I'm not sure I understand your question: yes, there is a default-value attribute for id-tags, as mentioned above. Is that what you wanted to know, or did I misunderstand you ?

X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 12:02 pm 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Oops, I just reread my own post ... I'm sorry, I understand your question now.

As far as I know, there's no default-value attribute, you should implement default values yourself in code.

X.


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