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:创建部分序列的 ...
随机推荐
- Liferay7 BPM门户开发之33: Portlet之间通信的3种方式(session、IPC Render Parameter、IPC Event、Cookies)
文章介绍了5种方式,4种是比较常用的: Portlet session IPC Public Render Parameters IPC Event Cookies 参考地址: https://web ...
- 常用基础OC 集合
// 2016年07月19日17:50:53 集合 //七.NSSet 集合对象(容器类,) // 1. 使用类方法创建对象 NSSet *set1 = [NSSet set]; // ...
- 炜煌T3POS58微打参数设置方法
本文用菊子曰发布
- 斐波那契数列(Fibonacci)递归和非递归实现
序列前9项为:0, 1, 1, 2, 3, 5, 8, 13, 21 要注意非递归的话就是那一个变量帮助存储当前下一项的值,然后依次挪动两个指针往下即可 注意如果n太大 会溢出 ...
- mssql 获取表空间大小
create table #Data(name varchar(100),row varchar(100),reserved varchar(100),data varchar(100),index_ ...
- mysql 修改root登录密码
mysql -u root -p 然后回车,进入(等于用空密码进入) 2 use mysql; update user set password=password('123456') where us ...
- ASP.NET弹出显示ex.Message异常信息 存在换行符和回车符处理办法。
1.把ex.Message换成任意字符串,检验在catch语句块中可以用Response.Write方法显示对话框.结果显示成功,说明问题就出在ex.Message上. 2.在程序中下断点,可以看到e ...
- C#使用Dotfuscator混淆代码以及加密
http://www.cnblogs.com/tianguook/archive/2012/10/06/2713105.html C#编写的代码如果不进行一定程度的混淆和加密,那么是非常容易被反编译进 ...
- 构造函数和:this()的应用
一.构造函数和:this()的应用 //本实例演示构造函数和:this()的应用 public class ClsA { public string A{set;get;} public string ...
- Quartz 2D绘制简单图形
在Quartz 2D中,绘图是通过图形上下文进行绘制的,以下绘制几个简单的图形 首先先创建一个QuartzView.swift文件继承自UIView,然后实现drawRect方法: import UI ...