NHibernate多对多-如何从连接表中检索属性并将其与子级关联? [英] NHibernate many-to-many - how to retrieve property from join table and associate it with a child?
问题描述
我在SQL中已有一个多对多关系,该关系已通过NHibernate映射到我的业务实体.
I have an existing many-to-many relationship in SQL that is being mapped to my business entities via NHibernate.
我想为子级添加一个属性(以下类别),该属性仅适用于父级和子级之间的关系.在SQL中,我会将一个字段添加到联接表中.
I want to add a property to the child (Category below) that is only applicable to the relationship between the parent and the child. In SQL, I would add a field to the join table.
如何使用NHibernate从联接表中提取该值并将其与孩子的财产相关联?
How do I use NHibernate to pull that value from the join table and associate it with a child's property?
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="MyProject.Core.Entities"
assembly="MyProject.Core">
<class name="Product" table="Products" lazy="false">
<id name="ProductId" access="field">
<generator class="native" />
</id>
<property name="ProductName" access="field" />
<idbag name="Categories" table="ProductCategory">
<collection-id column="ProductCategoryId" type="int">
<generator class="native" />
</collection-id>
<key column="ProductId" />
<many-to-many column="CategoryId" class="Category" />
</idbag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="MyProject.Core.Entities"
assembly="MyProject.Core">
<class name="Category" table="Categories" lazy="false">
<id name="CategoryId" access="field">
<generator class="native" />
</id>
<property name="CategoryName" access="field" />
</class>
</hibernate-mapping>
推荐答案
基本答案是必须声明联接表并为其创建一个类.在这里可以找到类似的问题:
The basic answer is that you have to declare your joining table and create a class for it. A similar question can be found here:
如何在连接表(Fluent NHibernate)上使用具有属性(列)的NHibernate ManyToMany
基于评论的说明:
Product one-to-many ProductCategory
Category one-to-many ProductCategory
ProductCategory many-to-one Product
ProductCategory many-to-one Category
这篇关于NHibernate多对多-如何从连接表中检索属性并将其与子级关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!