本文介绍了在nhibernate中保存具有多对一映射的数据时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个表DocumentType和EmployeeDocumentType. EmployeeDocumentType w.r.t DocumentType中有多行.

There are two tables DocumentType and EmployeeDocumentType. There are multiple rows in EmployeeDocumentType w.r.t DocumentType.

我需要在EmployeeDocumentType中使用多对一映射.我正在使用以下语法:

I need to use many to one mapping in EmployeeDocumentType. I am using following syntax:

<many-to-one name="DocumentType" column="DocumentTypeId" class="DocumentType" />

数据正确输入到EmployeeDocumentType对象中,但是当我将详细信息保存到EmployeeDocumentType中时,它给出了错误消息

Data is coming correctly in EmployeeDocumentType object, but when I save details into EmployeeDocumentType, it is giving error as,

可能是问题所在.

推荐答案

此问题与doubled映射有关(几乎可以肯定).

检查您的映射文件是否存在可以使用column="DocumentTypeId"的其他地方
(或默认为name="DocumentTypeId")

Check your mapping file for another place where the column="DocumentTypeId" could be used
(or by some default convention as name="DocumentTypeId")

<!-- example of doubled mapping could be Int property representing the Int column -->
<property    name="DocumentTypeId" column="DocumentTypeId" /> 
<many-to-one name="DocumentType"   column="DocumentTypeId" class="DocumentType" />

这里的解决方案很简单,其中之一必须标记为只读

The solution is easy here, one of these must be marked as readonly

<property    name="DocumentTypeId" column="DocumentTypeId"
                                                       insert="false" update="false"/> 
<many-to-one name="DocumentType"   column="DocumentTypeId" class="DocumentType" />

这篇关于在nhibernate中保存具有多对一映射的数据时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 04:34