NHibernate map one-to-many relationship with inter

2019-03-22 04:04发布

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)

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

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

mappings:

<?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>

1条回答
beautiful°
2楼-- · 2019-03-22 04:39

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. Collections of Values and Many-To-Many Associations

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) 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. Bidirectional Associations

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

  • 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. Author/Work (contains full example)

查看更多
登录 后发表回答