views:

1001

answers:

1

I'm newish to NHibernate, and am attempting to wire up to a DB2 table through the ISeries provider. The table has a BIGINT primary key that is auto generated as an identity. I've tried several values for the generator property of the id in my mapping file, and haven't had any success. The table def looks like so (field names changed):

CREATE TABLE SCHEMA/TABLE (
    PKID BIGINT GENERATED ALWAYS AS IDENTITY (
     START WITH 1 INCREMENT BY 1
     NO MINVALUE NO MAXVALUE
     NO CYCLE NO ORDER
     CACHE 20)
    ,
    SOMESTRING VARCHAR(50) CCSID 37 DEFAULT NULL,
    FIRSTFK BIGINT NOT NULL,
    SECONDFK BIGINT DEFAULT NULL,
    ANOTHERSTRING VARCHAR(100) CCSID 37 DEFAULT NULL,
    CONSTRAINT NISDEV/PK_TABLE PRIMARY KEY (PKID));

The mapping file looks like this:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Project.Domain.Thing, Project" table="TABLE">
    <id name="ID" column="PKID" type="Int64">
      <generator class="**???????**" />
    </id>
    <property name="SomeString" column="SOMESTRING" />
    <property name="FirstFK" column="FIRSTFK"/>
    <property name="SecondFK" column="SECONDFK"/>
    <property name="AnotherString" column="ANOTHERSTRING"/>
  </class>
</hibernate-mapping>

At first, I had the generator class set to "native," which, according to the documentation, picks "identity" for DB2. With "native" or "identity," I get "Null values not allowed in column PKID," and various other errors when I change the generator class to various other values.

I'm sure there's something small that I'm missing in the documentation, but is there any way I can get NHibernate to pick up the next value of a primary key that is a GENERATED IDENTITY in DB2 and handle it for me when I call Save()? Do I need to write a select somewhere that NHibernate can use to get the next value?

Thanks in advance.

A: 

I figured this out, so if anyone is doing this sort of thing in the future and runs into the same problem, here's what it was.

I was using the tutorial from the NHibernate FAQ and adapting it to ISeries. Following it to a T, I neglected to skip the part that actually creates the schema. I had all of my tables created already in the ISeries DB2 environment, so I should not have done this for my test runs:

<NUnit.Framework.SetUp()> _
Public Sub SetupContext()
     Dim schemaExport As New NHibernate.Tool.hbm2ddl.SchemaExport(_configuration)
     schemaExport.Execute(False, True, False, False)
End Sub

Especially since I had my mapping file set up incorrectly for the ID field, with a generator class of "assigned." Once I looked at my table definition and realized that NHibernate had overwritten the GENERATED IDENTITY attribute of the primary key field PKID when SchemaExport.Execute() was called, I simply recreated the table with the correct attributes, changed the generator class to "identity," removed the SchemaExport call, and now everything works perfectly.

AJ