gpt4 book ai didi

c# - NHibernate映射与中间表的一对多关系

转载 作者:行者123 更新时间:2023-12-02 00:16:21 25 4
gpt4 key购买 nike

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

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

我想要一个带有帖子类型标签的集合类(class):

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 指示该配对表。 (如果我们将标签分配到帖子中,我们不应该将此类映射标记为反向映射)

<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 (包含完整示例)

关于c# - NHibernate映射与中间表的一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30625609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com