NHibernate将多个表映射到一个类 [英] NHibernate Mapping multiple tables to one class

查看:121
本文介绍了NHibernate将多个表映射到一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的旧数据库中,我遇到这样的情况:

In my legacy Database I have a situation like this:

TableA (id_A[PK], cod_A)
TableB (id_B[PK], cod_B, id_A[FK])
TableC (id_C[PK], cod_C, id_B[FK])

出于多种原因,我需要将这些表映射到一个类中(在此示例中为Foo)

For several reasons I need to map these tables into a single class (Foo in this example)

public class Foo
{
    public virtual string IdA { get; set; }
    public virtual string CodA { get; set; }

    public virtual string IdB { get; set; }
    public virtual string CodB { get; set; }

    public virtual string IdC { get; set; }
    public virtual string CodC { get; set; }    
}

通过以下映射,我可以加入Table1和Table2,但不能加入Table3

By the following mapping I'm able to join Table1 and Table2 but not Table3

<class name="Foo" table="TableA">

    <id name="IdA" column="id_A"/>
    <property name="CodA" column="cod_A"/> 

    <join table="TableB">
      <key column="id_A"/>
      <property name="IdB" column="id_B"/>
      <property name="CodB" column="cod_B"/>      
    </join>

    <!--Here my problem because off course the join will be on TableA instead on TableB-->
    <join table="TableC">
      <key column="id_B"/>
      <property name="IdC" column="id_C"/>
      <property name="CodC" column="cod_C"/>      
    </join>

  </class>

如何映射Table3?

How can I map Table3?

谢谢.

推荐答案

NHibernate不鼓励您使用join.但是有时候没有办法解决.

NHibernate discourage you to use join as much as possible. But some times there is no way around it.

解决这个问题的第一个也是最简单的方法是创建一个视图并将您的类映射到该视图.

The first and easiest way to solve it, if you can, is to create a view and map your class to the view.

第二种方法是为每个其他表创建一个对象,并以正确的方式将它们连接起来,可能是<one-to-one>映射.

Second way, is to create an object to each of the other tables and connect them in the proper way, probably - <one-to-one> mapping.

第三种方式

*警告-后果自负*

下载NH源代码- https://github.com/Nicaog/nhibernate-core/downloads .

下载以下错误补丁修复说明- https://nhibernate.jira.com/browse/NH-1681

Download the following bug patch fix instructions - https://nhibernate.jira.com/browse/NH-1681

更改代码,编译DLL并将其添加到您的解决方案中.

Change the code, compile the DLL and add it to your solution.

说明-使用property-ref进行<join>映射的补丁程序得到了有效修复. ,但还要与property-ref一起使用多个联接.

Explanation - The patch actully fix using property-ref for <join> mapping. but also using multiple join with property-ref.

这篇关于NHibernate将多个表映射到一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆