代码之家  ›  专栏  ›  技术社区  ›  Mayo

如何从联接表中检索属性并将其与子级关联?

  •  3
  • Mayo  · 技术社区  · 14 年前

    我在SQL中有一个现有的多对多关系,正通过nhibernate映射到我的业务实体。

    我要向子级(下面的类别)添加一个仅适用于父级和子级之间关系的属性。在SQL中,我将向联接表添加一个字段。

    如何使用nhibernate从联接表中提取该值并将其与孩子的属性关联?

    <?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>
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   Community CDub    7 年前

    How to use NHibernate ManyToMany with properties (columns) on Join Table (Fluent NHibernate)

    Product one-to-many ProductCategory
    Category one-to-many ProductCategory
    ProductCategory many-to-one Product
    ProductCategory many-to-one Category