Nhibernate 具有相同Id密钥字段的多对多
如何将nHibernate中的多对一和一对多两个表连接起来。它们都具有与键列相同的“PLAN_ID”。我的数据库结构是预先存在的 下面是我正在使用的映射和我得到的错误: 表:计划(一个计划) Plann HBM XMLNhibernate 具有相同Id密钥字段的多对多,nhibernate,orm,Nhibernate,Orm,如何将nHibernate中的多对一和一对多两个表连接起来。它们都具有与键列相同的“PLAN_ID”。我的数据库结构是预先存在的 下面是我正在使用的映射和我得到的错误: 表:计划(一个计划) Plann HBM XML <class name="Plann" abstract="true" table="PLANN"> <id name="PlanId" column="PLAN_ID" <generator class="assigned"/&g
<class name="Plann" abstract="true" table="PLANN">
<id name="PlanId" column="PLAN_ID"
<generator class="assigned"/>
</id>
<property name="StartDate" column="START_DATE" />
<property name="CHECK_CHAR" column="CHECK_CHAR" />
...
<set name="PlannDocument" table="PLANN_DOCUMNET">
<key column="PLAN_ID"></key> //<<KEY IN JOINING TABLE BUT ALSO ID IN THIS TABLE
<one-to-many class="DocsysHoldingDoc"/>
</set>
</class>
<class name="PlannDocument" abstract="true" table="PLANN_DOCUMNET">
<id name="PlanId" column="PLAN_ID" type = "int">
<generator class="assigned"/>
</id>
<property name="DocId" column="DOC_ID" />
<property name="DocName" column="DOC_NAME" />
<property name="DocType" column="DOC_TYPE" />
<many-to-one name="Plann" column="PLAN_ID"></many-to-one>
</class>
根据您的评论(在“已删除的答案”下),两个表中的主键列的名称相同,即PLAN\u ID
。您希望基于多对一关系连接这两个表
这里的问题是相同的列名(来自两个不同的表)在映射中被添加了两次
这就是错误的原因:
错误:NHibernate.MappingException:“无法为类生成insert语句。PlannDocument:添加类的Id时出错”
ArgumentException:此SQL builder参数名称中已添加列“计划ID”:columnName
在PlannDocument HBM中,创建ID
时首先使用列名PLAN\u ID
,如下所示:
<id name="PlanId" column="PLAN_ID" type = "int">
<many-to-one name="Plann" column="PLAN_ID"></many-to-one>
这会导致列名冲突
我能想到的解决方案是将列名更改为另一个表中的延迟项。另外,相应地修改类和映射
我明白,这不可能每次都是所有情况下的解决方案。如果您的标识符和所需的多对一关联具有相同的列,那么它实际上不是多对一关联。共享相同的标识符
意味着它是一对一
关联。只需使用它,而不是PlannDocument.hbm.xml中的多对一
映射,它应该可以工作:
<one-to-one name="Plann" constrained="true" />
如果您的plannocument.Plann
可以为空,请改用constrained=“false”
。但是请注意,这将需要额外的查询来检查Plann
是否真的存在。这一点很好,但问题清楚地表明一个Plann
可能有许多Plann\u DOCUMNET
s。
<class name="PlannDocument" abstract="true" table="PLANN_DOCUMNET">
<id name="PlanId" column="PLAN_ID" type = "int">
<generator class="assigned"/>
</id>
<property name="DocId" column="DOC_ID" />
<property name="DocName" column="DOC_NAME" />
<property name="DocType" column="DOC_TYPE" />
<many-to-one name="Plann" column="PLAN_ID"></many-to-one>
</class>
<id name="PlanId" column="PLAN_ID" type = "int">
<many-to-one name="Plann" column="PLAN_ID"></many-to-one>
<one-to-one name="Plann" constrained="true" />