NHibernate映射与中间表一对多关系 [英] NHibernate map one-to-many relationship with intermediate table

查看:165
本文介绍了NHibernate映射与中间表一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不创建中间class PostTag的情况下定义映射?我有三张桌子

How to define mapping without intermediate class PostTag creation? I have three tables

t_post(id...)
t_tag(id, name)
t_post_tag(id,post_id, tag_id)

我想要一个具有Post类型标签的收藏集 课程:

I want to have a collection with Tags in Post type classes:

class Post
{
    public virtual IEnumerable<Tag> Tags{ get; set; }
}
public class Tag
{
}

映射:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Sample.Core" namespace="Sample.Core.Domain.Model" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Post" table="t_post" lazy="true" >
    <id name="Id" column="id" type="System.Int64" unsaved-value="-1"  generator="identity">
    </id>
...
    <bag name="Tags" lazy="true" cascade="none" inverse="true">
      <key column="post_id"/>
      <one-to-many class="Tag" />
    </bag>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Sample.Core" namespace="Sample.Core.Domain.Model" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Tag" table="t_tag" lazy="true" >
    <id name="Id" column="id" type="System.Int64" unsaved-value="-1"  generator="identity">
    </id>
  </class>
</hibernate-mapping>

推荐答案

要映射配对表而没有显式实体表示,我们必须使用<many-to-many>.还必须存在属性table="..."-以便指示NHibernate有关该配对表的信息. (如果我们将标签分配给帖子,我们应该将此类映射标记为反向)

To map pairing table, without explicit Entity representing it, we have to use <many-to-many>. Also attribute table="..." must be present - to instruct NHibernate about that pairing table. (In case, that we assign Tags into Posts, we should not mark such mapping as inverse)

<bag name="Tags" table="t_post_tag"
    lazy="true" cascade="none" inverse="true">
  <key column="post_id"/>
  <!--<one-to-many class="Tag" />-->
  <many-to-many class="Tag" column="tag_id"/>
</bag>

6.3.价值观和多对多联想

具有其自己的表的实体的集合对应于多对多关联的关系概念.多对多关联是.NET集合中最自然的映射,但通常不是最佳的关系模型.

A collection of entities with its own table corresponds to the relational notion of many-to-many association. A many to many association is the most natural mapping of a .NET collection but is not usually the best relational model.

<many-to-many
    column="column_name"                               (1)
    class="ClassName"                                  (2)
    fetch="join|select"                                (3)
    not-found="ignore|exception"                       (4)
/>

(1)列(必需):元素外键列的名称.
(2)class(必填):关联的类的名称.
(3)提取(可选,默认为联接):为此关联启用外部联接或顺序选择提取.这是一个特例;为了完全渴望获取(在单个SELECT中)实体及其与其他实体的多对多关系,您不仅要启用集合本身的联接获取,而且要启用<many-to-many>嵌套元素上的此属性.
(4)找不到(可选-默认为异常):指定将如何处理引用缺失行的外键:ignore会将缺失行视为空关联.

(1) column (required): The name of the element foreign key column.
(2) class (required): The name of the associated class.
(3) fetch (optional, defaults to join): enables outer-join or sequential select fetching for this association. This is a special case; for full eager fetching (in a single SELECT) of an entity and its many-to-many relationships to other entities, you would enable join fetching not only of the collection itself, but also with this attribute on the <many-to-many> nested element.
(4) not-found (optional - defaults to exception): Specifies how foreign keys that reference missing rows will be handled: ignore will treat a missing row as a null association.

6.8.双向关联

双向关联允许从关联的两端"进行导航.支持两种双向关联:

A bidirectional association allows navigation from both "ends" of the association. Two kinds of bidirectional association are supported:

  • one-to-many集或袋的一端为值,另一端为单值
  • many-to-many集或袋的两端均值
  • one-to-many set or bag valued at one end, single-valued at the other
  • many-to-many set or bag valued at both ends

23.2.作者/作品 (包含完整示例)

这篇关于NHibernate映射与中间表一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆