具有相同 ID 键字段的多对多 [英] Many-to-many with Same Id Key fields
问题描述
如何在 nHibernate 中以多对一和一对多的方式连接两个表.它们都具有与键列相同的PLAN_ID".我的数据库结构预先存在.
下面是我使用的映射和我得到的错误:
表:计划(一个)
PLAN_ID <-- PRIMARY KEY开始日期CHECK_CHAR...
表格:PLANN_DOCUMENT(到许多)
PLAN_ID <-- PRIMARY KEYDOC_ID <-- DOCUMENT 表的 IDDOC_NAMEDOC_TYPE
计划类:
public virtual... PlanId, StartDate, CheckChar, PlanStatus,公共虚拟 ISetDocsysHoldingDocs { 获取;放;}公共虚拟 DocsysHoldingDoc DocsysHoldingDoc { 获取;放;}
计划文档类:
public virtual...PlanId, DocId, DocName, DocType公共虚拟计划 PlannItem { get;放;}
规划 HBM XML
<id name="PlanId" column="PLAN_ID"<generator class="assigned"/></id><property name="StartDate" column="START_DATE"/><属性名称=CHECK_CHAR"列=CHECK_CHAR"/>...<set name="PlannDocument" table="PLANN_DOCUMNET"><key column="PLAN_ID"></key>//<<键在加入表中,但也在这个表中<一对多类="DocsysHoldingDoc"/></set></class>
计划文档 HBM XML
<id name="PlanId" column="PLAN_ID" type="int"><generator class="assigned"/></id><属性名称=DocId"列=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>
<块引用>
错误:NHibernate.MappingException: '无法为类 <>.PlannDocument 构建插入语句:添加类的 Id 时发生失败'
ArgumentException:已在此 SQL 构建器中添加了PLAN_ID"列 参数名称:columnName
我在这里做错了吗?
根据您的评论(在已删除的答案下),两个表中的主键列的名称相同 - 即 PLAN_ID
.您想基于多对一关系连接这两个表.
这里的问题是相同的列名(来自两个不同的表)在映射中被添加了两次.
这就是错误的原因:
<块引用>ERROR: NHibernate.MappingException: '无法为类 <>.PlannDocument 构建插入语句:添加类的 Id 时发生故障'
ArgumentException:列 'PLAN_ID' 已添加到此 SQL 构建器参数名称:columnName
在 PlannDocument HBM 中,列名 PLAN_ID
在创建 id
时首先使用,如下所示:
然后,再次创建多对一
关系如下:
这会导致列名冲突.
我能想到的解决方案是将列名更改为其他表延迟的内容.此外,相应地修改类和映射.
我知道这不可能在所有情况下都成为解决方案.
How do I join two tables as many-to-one and one-to-many in nHibernate. They both have the same 'PLAN_ID' as the key column. My Database structures pre-existing.
Below is the mapping I am using and the error I'm getting:
Table: PLANN (one)
PLAN_ID <-- PRIMARY KEY
START_DATE
CHECK_CHAR
...
Table: PLANN_DOCUMENT (to many)
PLAN_ID <-- PRIMARY KEY
DOC_ID <-- ID to a DOCUMENT table
DOC_NAME
DOC_TYPE
Plann class:
public virtual... PlanId, StartDate, CheckChar, PlanStatus,
public virtual ISet<DocsysHoldingDoc> DocsysHoldingDocs { get; set; }
public virtual DocsysHoldingDoc DocsysHoldingDoc { get; set; }
PlannDocument Class:
public virtual...PlanId, DocId, DocName, DocType
public virtual Plann PlannItem { get; set; }
Plann HBM XML
<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>
PlannDocument HBM XML
<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>
ERROR: NHibernate.MappingException: 'Unable to build the insert statement for class <>.PlannDocument: a failure occurred when adding the Id of the class'
ArgumentException: The column 'PLAN_ID' has already been added in this SQL builder Parameter name: columnName
Am I doing something wrong here?
Based on your comments (under deleted answer), name of primary key column in both the tables is same - i.e. PLAN_ID
. You want to join these two tables based on many-to-one relation.
The problem here is that the same column name (from two different tables) is being added twice in the mapping.
That is why the error:
ERROR: NHibernate.MappingException: 'Unable to build the insert statement for class <>.PlannDocument: a failure occured when adding the Id of the class'
ArgumentException: The column 'PLAN_ID' has already been added in this SQL builder Parameter name: columnName
In PlannDocument HBM, column name PLAN_ID
is first used while creating id
as below:
<id name="PlanId" column="PLAN_ID" type = "int">
and then, again while creating many-to-one
relation as below:
<many-to-one name="Plann" column="PLAN_ID"></many-to-one>
This is causing conflict of column name.
The solution I can think is to change the column name to something defer from the other table. Also, modify the classes and mappings accordingly.
I understand that this cannot be the solution every time and in all the conditions.
这篇关于具有相同 ID 键字段的多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!