NHibernate 使用CreateSQLQuery进行查询
涉及的表:
Cake{
Id ,
CakeName
}
CakeSize{
CakeId,-为外键,对应Cake表的字段Id
Size
}
(其中ISession session = NHibernateHelper.GetCurrentSession();)
用法一(返回数值):
ISQLQuery query = session.CreateSQLQuery("SELECT COUNT(Id) AS C FROM Cake").AddScalar("C", NHibernateUtil.Int32);
int c = Convert.ToInt32(query.UniqueResult());
或int c = query.UniqueResult<int>;//使用此方法发现sql执行了两次,故不推荐使用。
用法二(返回对象实体):
ISQLQuery query = session.CreateSQLQuery("select * from cake c").AddEntity("CAKE.DataTransfer.Entities.Cake");
或ISQLQuery query = session.CreateSQLQuery("select * from cake c").AddEntity("c", "CAKE.DataTransfer.Entities.Cake");
或ISQLQuery query = session.CreateSQLQuery("select * from cake c").AddEntity(typeof(Cake));
或ISQLQuery query = session.CreateSQLQuery("select * from cake c").AddEntity("c", typeof(Cake));
或ISQLQuery query = session.CreateSQLQuery("select * from cake c").AddEntity("c", typeof(Cake), LockMode.Write);
IList<Cake> c = query.List<Cake>();
用法三(连表查询):
ISQLQuery query = session.CreateSQLQuery("select cs.* from cake c join CakeSize cs on cs.CakeId=c.Id")
.AddEntity("cs", typeof(CakeSize));
IList<CakeSize> cs = query.List<CakeSize>();
以上是一种常见的用法,通过该例子,相信大家心里也已经有个数了,也应该知道怎么操作这个sql了:)
但是过程中难免会遇到些问题,比如:
实例二(问题说明):
我需要执行例如这条sql语句:
select CreateTime,Address,Password, (case when EmailType = 0 then 'Gmail' when EmailType = 1 then 'Yahoo' when EmailType = 2 then 'Hotmail' else 'Other' end) as EmailType from MailInfo
1. 如果使用session.CreateQuery方法执行的话,你可能会遇到这样的错误:
错误:undefined alias or unknown mapping
ISession session = DAORepository.Instrance.DbSession;
string queryString = "select CreateTime,Address,Password, (case when EmailType = 0 then 'Gmail' when EmailType = 1 then 'Yahoo' when EmailType = 2 then 'Hotmail' else 'Other' end) as EmailType from MailInfo where status=0 Order by CreateTime desc";
IQuery query = session.CreateQuery(queryString;
IList lst = query.List();
2. 使用CreateSQLQuery方法执行:
错误:Return types of SQL query were not specified...
ISession session = DAORepository.Instrance.DbSession;
string queryString = "select CreateTime,Address,Password, (case when EmailType = 0 then 'Gmail' when EmailType = 1 then 'Yahoo' when EmailType = 2 then 'Hotmail' else 'Other' end) as EmailType from MailInfo where status=0 Order by CreateTime desc";
ISQLQuery query = session.CreateSQLQuery(queryString);
IList lst = query.List();
3. 使用CreateSQLQuery方法并指定返回字段值的类型,即使用AddScalar方法指定字段值类型:
错误:could not execute query...
ISession session = DAORepository.Instrance.DbSession;
string queryString = "select CreateTime,Address,Password, (case when EmailType = 0 then 'Gmail' when EmailType = 1 then 'Yahoo' when EmailType = 2 then 'Hotmail' else 'Other' end) as EmailType from MailInfo where status=0 Order by CreateTime desc";
ISQLQuery query = session.CreateSQLQuery(queryString).AddScalar("CreateTime",NHibernateUtil.String)
.AddScalar("Address",NHibernateUtil.String)
.AddScalar("Password",NHibernateUtil.String)
.AddScalar("EmailType",NHibernateUtil.Int32);
IList lst = query.List();
ISession session = DAORepository.Instrance.DbSession;
string queryString = "select CreateTime,Address,Password, (case when EmailType = 0 then 'Gmail' when EmailType = 1 then 'Yahoo' when EmailType = 2 then 'Hotmail' else 'Other' end) as EmailType from MailInfo where status=0 Order by CreateTime desc";
ISQLQuery query = session.CreateSQLQuery(queryString).AddScalar("CreateTime",NHibernateUtil.String)
.AddScalar("Address",NHibernateUtil.String)
.AddScalar("Password",NHibernateUtil.String)
.AddScalar("EmailType",NHibernateUtil.Int32);
IList lst = query.List();
注意:EmailType字段对应数据库的类型是int,这里我就误解为也应该映射为NHibernateUtil.Int32;
4. 解决问题:
把AddScalar方法的参数里一个属性改了一下:
ISession session = DAORepository.Instrance.DbSession;
string queryString = "select CreateTime,Address,Password, (case when EmailType = 0 then 'Gmail' when EmailType = 1 then 'Yahoo' when EmailType = 2 then 'Hotmail' else 'Other' end) as EmailType from MailInfo where status=0 Order by CreateTime desc";
ISQLQuery query = session.CreateSQLQuery(queryString).AddScalar("CreateTime",NHibernateUtil.String)
.AddScalar("Address",NHibernateUtil.String)
.AddScalar("Password",NHibernateUtil.String)
.AddScalar("EmailType",NHibernateUtil.String);
IList lst = query.List();
ISession session = DAORepository.Instrance.DbSession;
string queryString = "select CreateTime,Address,Password, (case when EmailType = 0 then 'Gmail' when EmailType = 1 then 'Yahoo' when EmailType = 2 then 'Hotmail' else 'Other' end) as EmailType from MailInfo where status=0 Order by CreateTime desc";
ISQLQuery query = session.CreateSQLQuery(queryString).AddScalar("CreateTime",NHibernateUtil.String)
.AddScalar("Address",NHibernateUtil.String)
.AddScalar("Password",NHibernateUtil.String)
.AddScalar("EmailType",NHibernateUtil.String);
IList lst = query.List();
注意:这里我将EmailType的类型映射改为了NHibernateUtil.String,实际上是与返回结果的EmailType值类型保持一致就OK了。
NHibernate 使用CreateSQLQuery进行查询的更多相关文章
- 让OData和NHibernate结合进行动态查询
OData是一个非常灵活的RESTful API,如果要做出强大的查询API,那么OData就强烈推荐了.http://www.odata.org/ OData的特点就是可以根据传入参数动态生成Ent ...
- NHibernate的几种查询方式
SQL:普通SQL查询(也就是SQLServer,Oracle,Sybase等数据库的查询语句,建议使用基于TSQL-92) 核心对象:CreateSQLQuery IList list = sess ...
- [NHibernate]N+1 Select查询问题分析
目录 写在前面 文档与系列文章 N+1 Select查询问题分析 总结 写在前面 在前面的文章(延迟加载,立即加载)中都提到了N+1 Select的问题,总觉得理解的很不到位,也请大家原谅,这也是为什 ...
- Nhibernate中CreateSQLQuery用法实例
说明: 使用原生SQL查询时,若要通过addEntity方法引入对象,则查询结果列中必须包含该对象的所有属性,否则会抛出System.IndexOutOfRangeException异常. 结论: 若 ...
- nhibernate教程(4)--条件查询(Criteria Query)
NHibernate之旅(4):探索查询之条件查询(Criteria Query) 2008-10-16 18:20 by 李永京, 44341 阅读, 43 评论, 收藏, 编辑 本节内容 NHi ...
- Nhibernate Fluent INNER JOIN 查询
var list = session.QueryOver<PluginEntity>().JoinQueryOver(o => o.PluginModule, NHibernate. ...
- NHibernate系列学习(三)-条件查询Criteria
1.本笔记主要介绍Criteria的使用 2.效果界面 3.代码详情 namespace KimismeDemo { public partial class Form3 : Form { priva ...
- NHibernate - ICriteria 查询
http://blog.knowsky.com/213234.htm http://blog.chinaunix.net/uid-20463341-id-1673509.html http://www ...
- NHibernate 数据查询之QueryOver<T>
NHibernate 数据查询之QueryOver<T> 一.限制运算符 Where:筛选序列中的项目WhereNot:反筛选序列中的项目 二.投影运算符 Select:创建部分序列的 ...
随机推荐
- JProfiler8 注册码序列号
JProfiler_SN_8_x.txt 按默认选择"Single or evaluation license" Name 和 Company 随意 --------------- ...
- iOS App 研发的最后冲刺:内测与部署
当开发者历经磨难.披荆斩棘,完成了一个iOS项目后,最后的临门一脚就是应用的内测.部署.那么,在这最后的射门动作中,都有哪些地方需要开发者注意?有哪些方式能够更好地帮助我们进行iOS应用的发布部署? ...
- FIR.im Weekly - 技术是练出来的
本期 Weekly 主要精选了上周一些不错的 GitHub 资源.开发工具和技术实践教程类文章分享给大家. JSPatch – 动态更新 iOS APP JSPatch 是 @Bang 最近业余做的小 ...
- iOS数据持久化-OC
沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...
- linux平台下防火墙iptables原理(转)
原文地址:http://www.cnblogs.com/ggjucheng/archive/2012/08/19/2646466.html iptables简介 netfilter/iptables( ...
- ubuntu下安装oracle
本来打算在 redhat 下面安装了,但是没有 redhat 的盘了,就装了个 ubuntu 桌面版,结果安装起来十分费劲,主要是之前没安装过,对 oracle 的架构和配置也不太懂. 下面记录我安装 ...
- android studio 乱码
1. 设置 file- setting -file encodeing- 设置utf-8 2 . build.gradle 添加 tasks.withType(JavaCompile) { opti ...
- Think in java 4th读书笔记__last update20151130
一周至少两章,去掉最后的并发和图形化用户界面,刚好需要2个半月才能学好.这进度感觉有点慢,所以做下调整吧,改成一个月会不会更好点^^,认认真真的把java的圣经给看一遍. 计划: 第1~6 11.17 ...
- What is a Statistic?
from: https://controls.engin.umich.edu/wiki/index.php/Basic_statistics:_mean,_median,_average,_stand ...
- Scala 深入浅出实战经典 第41讲:List继承体系实现内幕和方法操作源码揭秘
Scala 深入浅出实战经典 第41讲:List继承体系实现内幕和方法操作源码揭秘 package com.parllay.scala.dataset /** * Created by richard ...