Nhibernate 一对多关联

发布于 2024-12-10 01:12:03 字数 1932 浏览 1 评论 0原文

我有这样一个简单的模型:

public abstract class Entity
{
    public virtual Guid Id { get; protected set;  }
}

public class Post : Entity
{
    public String Title { get ; set; }
    public String Content { get; set; }
    public DateTime Timestamp { get; set; }
    public Byte[] Thumbnail { get; set; }
}

public class Blog : Entity
{   
    public String Title { get; set; }
    public ISet<Post> Posts { get; set; }
}

然后我有这样的映射:

BLOG

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true assembly="Application.Domain" namespace="Application.Domain.Entities">
  <class name="Blog">
    <!-- id generator -->
    <id name="Id">
      <generator class="guid.comb" />
    </id>
  <!-- properties/columns -->
  <property name="Title" not-null="true" />
  <!-- components/columns -->
  <!-- associations -->
  <set name="Posts" cascade="all">
     <!-- key column? -->       
  </set>
  </class>
</hibernate-mapping>

POST

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="Application.Domain" namespace="Application.Domain.Entities">
  <class name="Blog">
    <!-- id generator -->
    <id name="Id">
      <generator class="guid.comb" />
    </id>
    <!-- properties/columns -->
    <property name="Title" not-null="true" />
    <property name="Content" not-null="true" />
    <property name="Timestamp" not-null="true"/>
    <property name="Thmbnail" />
    <!-- components/columns -->
    <!-- associations -->
  </class>
</hibernate-mapping>

如何映射一对多关联(单向)?

谢谢!

I have such a simple model:

public abstract class Entity
{
    public virtual Guid Id { get; protected set;  }
}

public class Post : Entity
{
    public String Title { get ; set; }
    public String Content { get; set; }
    public DateTime Timestamp { get; set; }
    public Byte[] Thumbnail { get; set; }
}

public class Blog : Entity
{   
    public String Title { get; set; }
    public ISet<Post> Posts { get; set; }
}

Then I have such mappings:

BLOG

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true assembly="Application.Domain" namespace="Application.Domain.Entities">
  <class name="Blog">
    <!-- id generator -->
    <id name="Id">
      <generator class="guid.comb" />
    </id>
  <!-- properties/columns -->
  <property name="Title" not-null="true" />
  <!-- components/columns -->
  <!-- associations -->
  <set name="Posts" cascade="all">
     <!-- key column? -->       
  </set>
  </class>
</hibernate-mapping>

POST

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="Application.Domain" namespace="Application.Domain.Entities">
  <class name="Blog">
    <!-- id generator -->
    <id name="Id">
      <generator class="guid.comb" />
    </id>
    <!-- properties/columns -->
    <property name="Title" not-null="true" />
    <property name="Content" not-null="true" />
    <property name="Timestamp" not-null="true"/>
    <property name="Thmbnail" />
    <!-- components/columns -->
    <!-- associations -->
  </class>
</hibernate-mapping>

How do I map one-to-many association (unidirectional)?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演多会厌 2024-12-17 01:12:03

在博客映射文件中,您需要在引用帖子表中的 Blog 实体的外键列之间定义一个一对多关系,假设它是 BlogId 并且您需要告诉他们这个一对多关系与哪个类相关,在您的情况下,这将是 Post 类,您需要用它的完全限定来定义它包含此类和逗号的命名空间,然后是程序集名称 下列的:

<set name="Posts" table="Post">
  <key column="BlogId"/>
  <one-to-many class="Application.Domain.Entities.Post, Application.Domain"/>
</set>

In the blog mapping file you need to define a one-to-many relation between the foreign key column that references the Blog entity in the post table say it is BlogId and you need to tell'em what class this one-to-many relation relates to, in your case this will be the Post class and you need to define it with it's fully qualified namespace that contains this class and a comma then the assembly name as following:

<set name="Posts" table="Post">
  <key column="BlogId"/>
  <one-to-many class="Application.Domain.Entities.Post, Application.Domain"/>
</set>
Saygoodbye 2024-12-17 01:12:03

我认为这与此处描述的问题相同。

而且,我认为您在映射 Post 类名称时犯了一个错误,不应该是 Blog。另外,在您的示例中,从帖子到博客没有关系。

I think it's the same problem as described here.

And also, I think you have a mistake in mapping of the Post - class name shouldn't be Blog. Also, there's no relation from Post to Blog in your example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文