使用Nhibernate hbm映射在具有相同ID的多个表中插入记录

时间:2018-01-11 11:07:46

标签: c# nhibernate nhibernate-mapping hbm hbmxml

我有两个表,表1有一个标识列(AttachmentID)和其他字段,表2有一个AttachmentID列,它是Table1.AttachmentID和PK(不是标识列)的FK,所以Table1.AttachmentID = Table2.AttachmentID ,表1中所有记录的记录可能会也可能不会出现。

当我使用Nhibernate实体类保存记录时,表1和表2中的记录正确插入正确的关系,但它也在表2中插入后执行SCOPE_IDENTITY(),因为表2没有任何标识列所以SCOPE_IDENTITY给出了null,因为这个完整的操作正在失败。

正在执行插入查询

exec sp_executesql N'INSERT INTO Table1 (FileName, GroupID, RelativeStorageLocation, FileSize, FileStatus, StorageTypeID, DocumentId, AccessKey, AttachmentSource, FileThumbnailID) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9); select SCOPE_IDENTITY()',N'@p0 nvarchar(4000),@p1 int,@p2 nvarchar(4000),@p3 bigint,@p4 int,@p5 int,@p6 int,@p7 nvarchar(4000),@p8 int,@p9 int',@p0=N'IMG_20180108_145645.jpg',@p1=52122,@p2=NULL,@p3=0,@p4=3,@p5=0,@p6=0,@p7=NULL,@p8=5,@p9=NULL    
exec sp_executesql N'INSERT INTO Table2 (DateTaken, ImageSource, ImageLocation, AttachmentID) VALUES (@p0, @p1, @p2, @p3); select SCOPE_IDENTITY()',N'@p0 datetime,@p1 nvarchar(4000),@p2 nvarchar(4000),@p3 int',@p0='2018-01-08 14:56:45',@p1=N'Xiaomi Redmi Note 4',@p2=N'Murzuq District, Libya',@p3=13045  

以下是Nhibernate hbm文件

表1(GenericAttachment)

<id name="_id" column="AttachmentID" access="field">
  <generator class="identity"/>
</id>

<property name="FileName" update="false"/>
<property name="GroupID"/>
<many-to-one name="GenericAttachmentGroup" class="GenericAttachmentGroup" column="GroupID" fetch="select" cascade="none" not-null="false" update="false" insert="false"/>
<property name="RelativeStorageLocation"/>
<property name="FileSize"/>
<property name="FileStatus" />
<property name="StorageTypeID" column="StorageTypeID" />
<property name="DocumentId" not-null="false"/>
<property name="AccessKey" not-null="false"/>
<property name="AttachmentSource" column="AttachmentSource" />
<many-to-one name="Thumbnail" class="FileInformation" column="FileThumbnailID" fetch="select" cascade="save-update" not-null="false" not-found="ignore"/>
<one-to-one lazy="proxy" name="Metadata" class="GenericAttachmentMatadataDetails" fetch="select" cascade="save-update" outer-join="true"  property-ref="GenericAttachment" />

表2(GenericAttachmentMatadataDetails)

<id name="_id" column="AttachmentID" access="field">
  <generator class="native"/>
</id>
<property name="DateTaken" column="DateTaken" />
<property name="ImageSource" column="ImageSource" />
<property name="ImageLocation" column="ImageLocation" />
<many-to-one name="GenericAttachment" class="Guru.Business.Entities.GenericAttachment, Guru.Business"  column="AttachmentID"  fetch="select" lazy="proxy" />

我在SQL Server 2012中使用C#。

由于

2 个答案:

答案 0 :(得分:1)

您将表2的ID映射为本机。这将使用底层数据库的默认实现。对于SQL服务器,这是身份。

  

天然

     

根据能力选择身份,序列或hilo   底层数据库。

您想要将映射更改为已分配并自行设置ID。

如果它确实是一对一的映射,那么请更改映射以反映该映射。

此外,您编写的映射也没有多大意义。您不能通过将AttachmentID添加为表2的PK来使子表具有重复的PK。

答案 1 :(得分:0)

生成器类“外来”解决了问题,更新了映射配置

表1(GenericAttachment)

<id name="_id" column="AttachmentID" access="field">
  <generator class="identity"/>
</id>
<property name="FileName" update="false"/>
<property name="GroupID"/>
<many-to-one name="GenericAttachmentGroup" class="GenericAttachmentGroup" column="GroupID" fetch="select" cascade="none" not-null="false" update="false" insert="false"/>
<property name="RelativeStorageLocation"/>
<property name="FileSize"/>
<property name="FileStatus" />
<property name="StorageTypeID" column="StorageTypeID" />
<property name="DocumentId" not-null="false"/>
<property name="AccessKey" not-null="false"/>
<property name="AttachmentSource" column="AttachmentSource" />
<many-to-one name="Thumbnail" class="FileInformation" column="FileThumbnailID" fetch="select" cascade="save-update" not-null="false" not-found="ignore"/>
<one-to-one lazy="proxy" name="Metadata" class="GenericAttachmentMetadataDetails" fetch="select" cascade="save-update" outer-join="true" property-ref="GenericAttachment"/>

表2(GenericAttachmentMatadataDetails)

<id name="_id" column="AttachmentID" access="field">
  <generator class="foreign">
        <param name="property">GenericAttachment</param>
   </generator>
</id>
<property name="DateTaken" column="DateTaken" />
<property name="ImageSource" column="ImageSource" />
<property name="ImageLocation" column="ImageLocation" />
<many-to-one name="GenericAttachment" access="property" class="GenericAttachment" fetch="select" insert="false" update="false" cascade="none" column="AttachmentID" unique="true" />