复杂的NHibernate组件映射

时间:2009-08-20 08:27:06

标签: nhibernate inheritance mapping components

编辑:我简化了问题,只留下真正困扰我的东西。

大家好,

我正在尝试进行以下映射。

在我的数据库中,我有一个名为“ReportRowValue”的表,其中包含以下列:

  • RowNumber
  • ColumnNumber
  • StringValue
  • LongValue
  • DateValue

在我的代码中,我希望通过从这一个表创建几个两个类来获得更有用的结构。我想这应该使用组件和继承来完成,但我没有设法创建一个工作的映射文件。我想在代码中看起来应该是这样的:

ReportRow

  • RowNumber
  • 值(下面的ReportValue集合)

ReportValue (作为抽象类)

  • ColumnNumber

ReportValueString / ReportValueLong / ReportValueDate(每个继承自ReportValue)

    <击>
  • 值(每个值都具有一种类型的Value属性)

这就是全部!

有没有人可以指出我如何创建一个nhibernate映射文件/文件呢?

谢谢,

明月

2 个答案:

答案 0 :(得分:0)

有几种工具可以为你映射和构建类,其中一种是

mygeneration 是软件http://sourceforge.net/projects/mygeneration/

在此页面中,您可以找到需要使用的模板 softwarehttp://www.mygenerationsoftware.com/TemplateLibrary/Archives/查询= NHibernate的

在mygeneration工具中有这个之后,你只能连接到你的数据库并为你生成

答案 1 :(得分:0)

不幸的是,您不能在组件中使用多态结构。但我真的不确定你是否需要它。

以下代码直接来自我的头脑,所以它肯定有错误或遗漏的东西,不会编译。但它应该显示方向:

public class ReportRow
{
  public int Id { get; private set; }
  public IList<IReportValue> Values { get; private set; }
}

public interface IReportValue
{
  public int Id{ get; set; }
  public object UntypedValue { get; }
}

public abstract class ReportValue<T> : IReportValue
{
  public int Id{ get; set; }
  public T Value { get; set; }
  public object UntypedValue { get { return Value; } }
}

public class ReportLongValue : ReportValue<long> {}
public class ReportStringValue : ReportValue<string> {}
public class ReportDateValue : ReportValue<DateTime>{}

映射:

<class ReportRow>
  <id ...>
  <bag name="Values" >
    <key column="RowNumber"/>
    <one-to-many class="IReportValue"/>
  </bag>
</class>

<class name="IReportValue" abstract="true">
  <id ...>
  <subclass name="ReportLongValue">
    <property name="Value" column="LongValue"/>
  </subclass>
  <subclass name="ReportStringValue">
    <property name="Value" column="StringValue"/>
  </subclass>
  <subclass name="ReportDateValue">
    <property name="Value" column="DateValue"/>
  </subclass>
</class>