Nhibernate多表查询解决办法

概述:

    在项目中应用NHibernate架构时,会经常遇到多表查询.因为项目上要用到,通过多天的查询总结了以下两种解决方案.

解决方案:

  (1)使用HQL语句,然后把查询出的结果存入临时表中,然后再进行绑定.

  (2)使用select new OaxtJoinGlxt这种语法,根据查询的内容自己构建相应的实体类.

第一种方案实现具体过程:

我的需求如下:

   我需要查询JkptOaxtOrganization实体类中的Orgid,及JkptGlxtOrganization实体类中的Orgname我需要查询JkptOaxtOrganization实体类中的Orgid,及JkptGlxtOrganization实体类中的Orgname

第一步:构建HQL语句如下

  string sql = "select oo.Orgid ,go.Orgname from JkptOaxtOrganization as oo,JkptGlxtOrganization as go where oo.Orgid="+orgid+and oo.Suborgid=go.Orgid"; 
                IQuery Query 
=
 session.CreateQuery(sql);
 IList list 
= Query.List();

说明:如果在相应的映射文件中设置好实体的对应关系,就没有必要加Where条件了.

第二步:建立临时表,把查询出的结果存入临时表

DataTable dt = new DataTable();
                DataColumn dc 
= new DataColumn();  

                dc 
= new DataColumn(); //增加第1列
                dc.DataType = System.Type.GetType("System.Int32");
                dc.ColumnName 
= "Orgid";
                dt.Columns.Add(dc);

                dc 
= new DataColumn();  //增加第2列
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName 
= "Orgname";
                dt.Columns.Add(dc);
                dc.Dispose();

                IEnumerator enu 
= list.GetEnumerator();
                
while (enu.MoveNext())
                {     
//给查询出来的每行赋值
                    object[] obj = (object[])enu.Current;
                    DataRow newrow 
= dt.NewRow();
                    newrow[
"Orgid"= obj[0];
                    newrow[
"Orgname"= obj[1];
                    dt.Rows.Add(newrow);
                }

                ds.Tables.Add(dt);

我个人觉得,这种方式存在很大缺陷,如临时表用完需要删除,如果要查询50个字段,那么写临时表就得累死,所以我还是推荐大家用第二种方案.

 第二种方案的具体实现过程

第一步:构建HQL查询语句:

  string sql = "select new OaxtJoinGlxt(oo.Orgid,gg.Orgname)  from JkptOaxtOrganization as oo , JkptGlxtOrganization as gg where oo.Orgid=+ orgid + " and oo.Suborgid=gg.Orgid"; 
            IQuery query 
=
 session.CreateQuery(sql);
            IList list 
=
 query.List();
            ds 
=
 Helpers.ConvertToDataSet(list);
            
return ds;

第二步:根据查询的内容构造的实体类OaxtJoinGlxt.cs如下  

public  class OaxtJoinGlxt
    {
       
public OaxtJoinGlxt(decimal pOrgid, string pOrgname)
       {
           
this._Orgid = pOrgid;
           
this._Orgname = pOrgname;
       }

       
private decimal? _Orgid;
       
public decimal? Orgid
       {
           
get { return _Orgid; }
           
set { _Orgid =value;}
       }

       
private string _Orgname;
       
public string Orgname
       {
           
get { return _Orgname; }
           
set { _Orgname = value; }
       }
    }

 第三步:在相应的映射文件中导入OaxtJoinGlxt类

为了要让NHibernate知道到什么地方去找这个OaxtJoinGlxt类,我们需要将OaxtJoinGlxt类也导入到hbm.xml 文件定义中,在JkptOaxtWeatherforecast.hbm.xml中添加下面的语句就可以了:    

<?xml version="1.0" encoding="utf-8"?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  
<import  class="Model.OaxtJoinGlxt,Model"/>
  
<!--<import  class="Model.WeatherforecastJoinOrganization,Model"/>-->
  
<class name="Model.JkptGlxtOrganization,Model" table="JKPT_GLXT_ORGANIZATION" lazy="false">

到此,已经OK了,大家看一下运行的结果.

运行结果:     

 

原文地址:https://www.cnblogs.com/abcdwxc/p/1403647.html