首页 新闻 会员 周边 捐助

nHibernate中使用CreateSQLQuery提示不能在此泛型集合中使用

0
悬赏园豆:30 [已关闭问题] 关闭于 2014-03-04 10:37

最近才开始接触HQL,不是很了解,请大家帮忙看看,要怎么解决之~3Q!

问题描述:在执行下列代码时出现异常为:[值“System.Object[]”不是“OA.Model.TIPTOP.CPF_FILE”类型,不能在此泛型集合中使用。
参数名: value]

我用的是纯SQL,也请在此基础上稍作提示。

public virtual IList<Model.TIPTOP.AIMQ_FILE> ExpBlance(string where, int start, int length)
       {

           string ua = " union all";
           IList<Model.TIPTOP.AIMQ_FILE> ret =new List<Model.TIPTOP.AIMQ_FILE>() ;
           string hql = "SELECT oeb15 as DS_DATE,'' as DS_CLASS,oeb01 as DS_NO,occ02 as DS_CUST,(oeb12-oeb24)*oeb05_fac as DS_QLTY,0 as DS_TOTAL FROM oeb_file,oea_file,occ_file ";
           hql += " WHERE oeb01 = oea01 AND occ01 = oea03 AND oea00<>'0' AND oeb70 = 'N' AND oeb12 > oeb24 AND oeaconf !='X' and  oeb04 = '" + where + "'";
           hql += ua + "SELECT sfb15 as DS_DATE,'' as DS_CLASS,sfb01 as DS_NO,gem02 as DS_CUST,(sfb08-sfb09-sfb10-sfb11-sfb12)*1 as DS_QLTY,0 as DS_TOTAL FROM sfb_file,gem_file ";
           hql += " WHERE  sfb04 !='8' AND sfb82 = gem_file.gem01(+) AND sfb02 != '11' AND sfb08 > (sfb09+sfb10+sfb11+sfb12) AND sfb87!='X' and sfb05 ='" + where + "'";
           hql += ua + "SELECT pml33 as DS_DATE,'' as DS_CLASS,pml01 as DS_NO,pmc03 as DS_CUST,((pml20-pml21)*pml09) as DS_QLTY,0 as DS_TOTAL FROM pml_file,pmk_file,pmc_file ";
           hql += " WHERE  pml01 = pmk01 AND pmk09 = pmc_file.pmc01(+) AND pml20 > pml21 AND (pml16<='2' OR pml16='S' OR pml16='R' OR pml16='W') AND pml011 !='SUB' AND pmk18 != 'X' and pml04 = '" + where + "'";
           hql += ua + "SELECT pmn33 as DS_DATE,'' as DS_CLASS,pmm01 as DS_NO,pmc03 as DS_CUST,((pmn20-pmn50+pmn55)*pmn09) as DS_QLTY,0 as TOTAL FROM pmn_file,pmm_file,pmc_file ";
           hql += " WHERE pmn01 = pmm01 AND pmm09 = pmc_file.pmc01(+) AND pmn20 -(pmn50-pmn55)>0 AND (pmn16 <= '2' OR pmn16='S' OR pmn16='R' OR pmn16='W') AND pmn011 !='SUB' AND pmm18 != 'X' and pmn04 = '" + where + "'";
           hql += ua + "SELECT rva06 as DS_DATE,'' as DS_CLASS,rvb01 as DS_NO,pmc03 as DS_CUST,((rvb07-rvb29-rvb30)*pmn09) as DS_QLTY,0 as DS_TOTAL FROM rvb_file,rva_file,pmc_file,pmn_file ";
           hql += " WHERE rvb01 = rva01 AND rva05 = pmc_file.pmc01(+) AND rvb04 = pmn_file.pmn01 AND rvb03 = pmn_file.pmn02 AND rvb07 > (rvb29+rvb30) AND rvaconf='Y' and rvb05 ='" + where + "'";
           hql += ua + "SELECT sfb15 as DS_DATE,'' as DS_CLASS,sfb01 as DS_NO,gem02 as DS_CUST,sfb11 as DS_QLTY,0 as DS_TOTAL FROM sfb_file,gem_file ";
           hql += " WHERE sfb02 <> '7' AND sfb87!='X' AND sfb04 <'8' AND sfb82=gem_file.gem01(+) AND sfb11 > 0 and  sfb05 = '" + where + "'";
           hql += ua + "SELECT sfb13 as DS_DATE,'' as DS_CLASS,sfa01 as DS_NO,gem02 as DS_CUST,((sfa05-sfa06-sfa065)*sfa13) as DS_QLTY,0 as DS_TOTAL FROM sfb_file,sfa_file,gem_file ";
           hql += " WHERE sfb01 = sfa01 AND sfb82 = gem_file.gem01(+) AND sfb04 !='8' AND sfa05 > 0 AND sfa05 > sfa06+sfa065 AND sfb87!='X'and sfa03 = '" + where + "'";
           ISQLQuery query = isession.CreateSQLQuery(hql);
           //IQuery query = isession.CreateQuery(hql);
           if (start > 0) query.SetFirstResult(start);
           if (length > 0) query.SetMaxResults(length);
           //IList list = (IList)query.List();
           ret =(List<Model.TIPTOP.AIMQ_FILE>)query.List<Model.TIPTOP.AIMQ_FILE>();
           return ret;
       }

异常部分:行32,
异常详细信息: System.ArgumentException: 值“System.Object[]”不是“OA.Model.TIPTOP.CPF_FILE”类型,不能在此泛型集合中使用。
参数名: value

源错误:

行 30:          if (start > 0) query.SetFirstResult(start);
行 31:          if (length > 0) query.SetMaxResults(length);
行 32:          ret=query.List<Model.TIPTOP.CPF_FILE>();
行 33:          return ret;
行 34:        }
小熊QQ糖的主页 小熊QQ糖 | 初学一级 | 园豆:152
提问于:2013-12-02 11:28
< >
分享
所有回答(3)
0

 你这个问题原因是因为他返回的类型是object[]类型,并不是实体类型。所以这里改成:ret = query.List<object[]>();执行应该就能成功了

kuangkro | 园豆:241 (菜鸟二级) | 2013-12-06 13:39

不行哦,这个我试过的,必须要强制类型转换,而且我是想要得到list集合的,并不是object

支持(0) 反对(0) 小熊QQ糖 | 园豆:152 (初学一级) | 2013-12-06 13:42

@Annayang: 你的意思是说,强制转换成Model.TIPTOP.CPF_FILE能够成功?   你返回的集合应该就是object[]类型吧,不然就不会报你贴出的那个异常了

支持(0) 反对(0) kuangkro | 园豆:241 (菜鸟二级) | 2013-12-06 17:24

@kuangkro: 不能啊,所以我想說這裏強制類型轉換有如上錯誤提示,我想得到的是申明的那個ret類型[見貼圖,有定義],而按你說的<object[]>是可以成供,但違背了我的本意ILIST<MODEL>,懂否?

支持(0) 反对(0) 小熊QQ糖 | 园豆:152 (初学一级) | 2013-12-06 17:28

@Annayang: 你想要的到强类型的数据,那你返回回来的对象是不是这个强类型?我看你的sql,select出来的很多列都是来源于不同的表。就算赛选出来的列都是这个类型的属性,但是你这个类型不是持久化对象,ORM框架也没办法反射成对应的强类型

支持(0) 反对(0) kuangkro | 园豆:241 (菜鸟二级) | 2013-12-06 17:32

@kuangkro: 嗯啊,現在想到的是將其該寫成HQL,我想說除了這個方法外還有能通過SQL直接得到的方式麼

支持(0) 反对(0) 小熊QQ糖 | 园豆:152 (初学一级) | 2013-12-06 17:36

@Annayang: 如果你的聚合根里面包含其他对象的引用,可以采用QueryOVer()的方式来写。QueryOver()的where条件也可以写sql。现在复杂的sql采用QueryOver()方式是比较好的。

支持(0) 反对(0) kuangkro | 园豆:241 (菜鸟二级) | 2013-12-06 17:39
0

可以通过CreateSQLQuery(hql),来实现

小熊QQ糖 | 园豆:152 (初学一级) | 2014-03-04 10:37
0

我也遇到这个问题了,找不到原因,改成dao.NSession.CreateQuery(hql).List<T>()吧!

xuguanbao | 园豆:202 (菜鸟二级) | 2015-06-05 15:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册