NHibernate继承问题

编程入门 行业动态 更新时间:2024-10-20 20:46:38
本文介绍了NHibernate继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目前我有以下课程:

带有属性ID,标题和正文的类文章 课堂问题:带有额外的发布属性的文章

class Article with properties id, title and body class Question : Article with an extra PostedBy property

然后,我有了一个具有上述属性的名为Article的表,以及一个名为ID的问题表,该ID的ID为外键articleID,且为PostedBy.两者都处于不同的模式

Then I have a table called Article with the above properties and a table called questions with an ID a foreign key articleID and a PostedBy. Both are in different schemas

我想知道我的映射看起来如何代表这种关系.这两个类位于不同的程序集中,我非常不愿意将Question逻辑放在Article类/映射及其程序集中.

I would like to know how are my mappings going to look to represent this relation. Both classes are in different assemblies and i would be very reluctant to put Question logic in Article class/mapping and its assembly.

推荐答案

NHibernate支持三种基本的继承策略.

NHibernate supports three basic inheritance strategies.

  • 每个类层次结构的表
  • 每个子类的表
  • 每个具体类别的表格
  • 听起来好像您正在按子类策略查找表,因为您有Article类的表,而Question子类具有其他属性的表.映射可能看起来像这样:

    It sounds like you are looking for the table per subclass strategy as you have a table for your Article class and another table for the extra properties on the Question subclass. The mapping might looks something like this:

    <class name="Article" table="Article"> <id name="Id" type="Int64" column="ArticleId"> <generator class="native"/> </id> <property name="Title" column="Title"/> <property name="Body" column="Body"/> ... <joined-subclass name="Question" table="Question"> <key column="ArticleId"/> <property name="PostedBy" column="PostedBy"/> ... </joined-subclass> </class>

    但是,这不能满足您将映射完全分开的需求.您可能具有完全独立的映射,但是这可能会带来一些副作用,如允许将Question作为普通文章而不是Question加载.使用单独的映射,Article类将如预期的那样直接进行. Question类将包含用于访问存储在Article表中的属性的联接.

    However, this doesn't meet your desire to keep the mappings entirely separate. You could have entirely separate mappings, but this might have some side effects as allowing Question to be loaded as a plain Article instead of a Question. With separate mapping the Article class would be straight-forward as expected. The Question class would include a join to access the properties stored in the Article table.

    <class name="Article" table="Article"> <id name="Id" type="Int64" column="ArticleId"> <generator class="native"/> </id> <property name="Title" column="Title"/> <property name="Body" column="Body"/> ... </class> <class name="Question" table="Question"> <id name="Id" type="Int64" column="QuestionId"> <generator class="native"/> </id> <property name="PostedBy" column="PostedBy"/> ... <join table="Article"> <key column="ArticleId"/> <property name="Title" column="Title"/> <property name="Body" column="Body"/> </join> </class>

    更多推荐

    NHibernate继承问题

    本文发布于:2023-11-17 02:20:13,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1608458.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:NHibernate

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!