NHibernate 映射一对多关系与中间 table
NHibernate map one-to-many relationship with intermediate table
如何在没有中间 class PostTag
创建的情况下定义映射?我有三张桌子
t_post(id...)
t_tag(id, name)
t_post_tag(id,post_id, tag_id)
我想要一个包含 Post 类型标签的集合
类:
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>
要映射配对 table,没有显式实体表示它,我们必须使用 <many-to-many>
。属性 table="..."
也必须存在 - 以指示 NHibernate 关于该配对 table。 (如果我们将标签分配给帖子,我们应该不将这种映射标记为反向)
<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 (包含完整示例)
如何在没有中间 class PostTag
创建的情况下定义映射?我有三张桌子
t_post(id...)
t_tag(id, name)
t_post_tag(id,post_id, tag_id)
我想要一个包含 Post 类型标签的集合 类:
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>
要映射配对 table,没有显式实体表示它,我们必须使用 <many-to-many>
。属性 table="..."
也必须存在 - 以指示 NHibernate 关于该配对 table。 (如果我们将标签分配给帖子,我们应该不将这种映射标记为反向)
<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 othermany-to-many
set or bag valued at both ends
23.2. Author/Work (包含完整示例)