NHibernate:在SQL视图中映射一对多关系
我使用的是SQL视图,但我对NHibernate、视图或数据库的总体了解不够,无法知道问题所在。我正在使用的视图过去有一个字符串列,“ExemptionCode”。现在,视图可能有许多豁免代码。以下是新关系的XML映射:NHibernate:在SQL视图中映射一对多关系,nhibernate,nhibernate-mapping,Nhibernate,Nhibernate Mapping,我使用的是SQL视图,但我对NHibernate、视图或数据库的总体了解不够,无法知道问题所在。我正在使用的视图过去有一个字符串列,“ExemptionCode”。现在,视图可能有许多豁免代码。以下是新关系的XML映射: <class name="LatestDocumentVersion" table="LatestDocumentVersion" mutable="false" schema-action="none"> <id name="DocumentVers
<class name="LatestDocumentVersion" table="LatestDocumentVersion" mutable="false" schema-action="none">
<id name="DocumentVersionID" type="Int32"/>
<property name="ContainerDocumentID" type="Int32"/>
<!--<property name="ExemptionCode" length="10" />-->
<set name="ExemptionCodes" cascade="all-delete-orphan" lazy="false" inverse="false">
<key column="ContainerDocumentID"/>
<one-to-many class="ContainerDocumentExemptions"/>
</set>
<--Properties omitted-->
</class>
以下是ContainerDocumentExceptions类的映射:
<class name ="ContainerDocumentExemptions" lazy ="false">
<id name ="ContainerDocumentExemptionID" type="Int32">
<generator class="identity"/>
</id>
<many-to-one name="ContainerDocumentID" class="ContainerDocuments" column="ContainerDocumentID" cascade="none"/>
<property name="ExemptionCode" length="10"/>
</class>
ContainerDocumentException类实际上与ContainerDocument对象具有双向一对多关系。这是另一端:
<class name="ContainerDocuments" lazy="false" table="ContainerDocuments">
<id name="ContainerDocumentID" type="Int32">
<generator class="identity"/>
</id>
<!--<property name="ExemptionCode" length="10" />-->
<set name="Exemptions" cascade="all-delete-orphan" lazy="false" inverse="true">
<key column="ContainerDocumentID"/>
<one-to-many class="ContainerDocumentExemptions"/>
</set>
<--Properties omitted-->
</class>
将此行添加到ContainerDocuments类后,ContainerDocuments可以正确地写入和读取新ContainerDocuments表:
public class ContainerDocuments {
public virtual ISet<ContainerDocumentExemptions> Exemptions { get; set; }
//Properties omitted
}
公共类容器文档{
公共虚拟ISet{get;set;}
//省略属性
}
因此,我将此代码添加到LatestDocumentVersion类中:
public class LatestDocumentVersion {
public virtual int ContainerDocumentID { get; set; }
public virtual ISet<ContainerDocumentExemption ExemptionCodes { get; set; }
//properties omitted
}
公共类最新文档版本{
公共虚拟int ContainerDocumentID{get;set;}
公共虚拟ISetContainerDocumentID
是ContainerDocument
类上的id,但它不在LatestDocumentVersion
类上,默认情况下,每个对多的联接都会返回到id上。将属性ref
添加到LatestDocumentVersion
映射中
<class name="LatestDocumentVersion" table="LatestDocumentVersion" mutable="false" schema-action="none">
<id name="DocumentVersionID" />
<property name="ContainerDocumentID"/>
<set name="ExemptionCodes" cascade="all-delete-orphan" lazy="false" inverse="false">
<key column="ContainerDocumentID" property-ref="ContainerDocumentID"/>
<one-to-many class="ContainerDocumentExemptions"/>
</set>
</class>
成功!非常感谢!