NHibernate - 将 ID 映射到 DB2 身份

发布于 2024-07-13 22:32:50 字数 1566 浏览 9 评论 0原文

我对 NHibernate 很陌生,并且正在尝试通过 ISeries 提供程序连接到 DB2 表。 该表有一个自动生成的 BIGINT 主键作为标识。 我已经在映射文件中尝试了 id 的生成器属性的多个值,但没有取得任何成功。 表 def 看起来像这样(字段名称已更改):

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));

映射文件看起来像这样:

<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> 

首先,我将生成器类设置为“native”,根据 文档,为 DB2 选择“身份”。 对于“native”或“identity”,当我将生成器类更改为各种其他值时,我会收到“PKID 列中不允许空值”以及各种其他错误。

我确信文档中遗漏了一些小东西,但是有什么方法可以让 NHibernate 获取主键的下一个值(DB2 中的生成标识)并在我调用时为我处理它节省()? 我是否需要在 NHibernate 可以用来获取下一个值的地方编写一个选择?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花期渐远 2024-07-20 22:32:50

我明白了这一点,所以如果将来有人做这种事情并遇到同样的问题,这就是问题所在。

我正在使用 NHibernate FAQ 并使其适应 ISeries。 一直到最后,我忽略了跳过实际创建模式的部分。 我已经在 ISeries DB2 环境中创建了所有表,因此我不应该在测试运行中执行此操作:

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

特别是因为我的 ID 字段映射文件设置不正确,生成器类为“已分配”。 一旦我查看了表定义并意识到 NHibernate 在调用 SchemaExport.Execute() 时覆盖了主键字段 PKID 的 GENERATED IDENTITY 属性,我只需使用正确的属性重新创建表,将生成器类更改为“identity, " 删除了 SchemaExport 调用,现在一切正常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文