Castle ActiveRecord学习实践(7):使用HQL查询
2006-04-12 08:50
686 查看
摘要:虽然ActiveRecord为我们提供了Find()和FindAll()这样两个静态的查询方法,并且有Where特性可供使用,但是仍然不能解决实际开发中一些复杂的查询,这时我们就需要通过HQL查询来实现。
主要内容[/b][/b]
1.HQL概述
2.SimpleQuery查询
3.ScalarQuery查询
4.自定义查询
5.使用CallBack
一.[/b]HQL[/b]简单介绍[/b][/b]
HQL全名是Hibernate Query Language,它是一种完全面向对象的查询语言。先来看一下HQL最基本的一些用法
1.From子句
from Post
你也可以为Post起一个别名
from Post as post
或者省略as
from Post post
2.Select 子句
select Name,Author from Blog
也可以使用elements函数来查询一个集合
select elements(blog.Posts) from Blog blog
3.使用聚合函数
HQL中也可以使用一些聚合函数
select count(*) from Blog blog
select count(elements(blog.Posts)) from Blog blog
HQL支持的聚合函数有
avg(
count(*)
count(from Blog blog where blog.Name = ‘Terry Lee’
from Blog blog where blog.Name is not null
详细可以参考http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html。
二.SimpleQuery查询[/b]
SimpleQuery是一种最简单的查询,它直接处理HQL语句,并返回一个集合,没有复杂的参数处理。具体用法可以参考下例:
[ActiveRecord("Posts")]
public class Post : ActiveRecordBase
[Test]
public void TestGetPostsByCategory()
[Test]
public void TestGetPostsInterval()
[ActiveRecord("Blogs")]
public class Blog : ActiveRecordBase
[Test]
public void TestGetPostNumByAuthor()
public class QueryWithNamedParameters : ActiveRecordBaseQuery
public static Blog[] GetThreeBlogsFromAuthor( string authorName )
[Test]
public void TestCustomQuery()
[ActiveRecord("Blogs")]
public class Blog : ActiveRecordBase
[Test]
public void TestGetPostsFromAuthor()
{
string _StrAuthor = "Terry Lee";
IList list = Blog.GetPostsFromAuthor(_StrAuthor);
int expectedCount = 4;
Assert.IsNotNull(list);
Assert.AreEqual(expectedCount,list.Count);
}
关于使用HQL查询就介绍到这儿了,相信通过HQL查询可以解决我们开发中的绝大多数的复杂查询问题。
参考资料[/b]
文中主要内容来自于Castle的官方网站http://www.castleproject.org
主要内容[/b][/b]
1.HQL概述
2.SimpleQuery查询
3.ScalarQuery查询
4.自定义查询
5.使用CallBack
一.[/b]HQL[/b]简单介绍[/b][/b]
HQL全名是Hibernate Query Language,它是一种完全面向对象的查询语言。先来看一下HQL最基本的一些用法
1.From子句
from Post
你也可以为Post起一个别名
from Post as post
或者省略as
from Post post
2.Select 子句
select Name,Author from Blog
也可以使用elements函数来查询一个集合
select elements(blog.Posts) from Blog blog
3.使用聚合函数
HQL中也可以使用一些聚合函数
select count(*) from Blog blog
select count(elements(blog.Posts)) from Blog blog
HQL支持的聚合函数有
avg(
count(*)
count(from Blog blog where blog.Name = ‘Terry Lee’
from Blog blog where blog.Name is not null
详细可以参考http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html。
二.SimpleQuery查询[/b]
SimpleQuery是一种最简单的查询,它直接处理HQL语句,并返回一个集合,没有复杂的参数处理。具体用法可以参考下例:
[ActiveRecord("Posts")]
public class Post : ActiveRecordBase
[Test]
public void TestGetPostsByCategory()
[Test]
public void TestGetPostsInterval()
[ActiveRecord("Blogs")]
public class Blog : ActiveRecordBase
[Test]
public void TestGetPostNumByAuthor()
public class QueryWithNamedParameters : ActiveRecordBaseQuery
public static Blog[] GetThreeBlogsFromAuthor( string authorName )
[Test]
public void TestCustomQuery()
[ActiveRecord("Blogs")]
public class Blog : ActiveRecordBase
[Test]
public void TestGetPostsFromAuthor()
{
string _StrAuthor = "Terry Lee";
IList list = Blog.GetPostsFromAuthor(_StrAuthor);
int expectedCount = 4;
Assert.IsNotNull(list);
Assert.AreEqual(expectedCount,list.Count);
}
关于使用HQL查询就介绍到这儿了,相信通过HQL查询可以解决我们开发中的绝大多数的复杂查询问题。
参考资料[/b]
文中主要内容来自于Castle的官方网站http://www.castleproject.org
相关文章推荐
- C#.Net Castle ActiveRecord学习实践(7):使用HQL查询
- Castle ActiveRecord学习实践(7):使用HQL查询
- Castle ActiveRecord学习实践(9):使用ActiveRecord的一些技巧
- Castle ActiveRecord学习实践(6):延迟加载和使用Where子句
- C#.Net Castle ActiveRecord学习实践(9):使用ActiveRecord的一些技巧
- Castle ActiveRecord学习实践(6):延迟加载和使用Where子句
- Castle ActiveRecord学习实践(7):使用HQL查询
- Castle ActiveRecord学习实践(6):延迟加载和使用Where子句
- C#.Net Castle ActiveRecord学习实践(6):延迟加载和使用Where子句
- Castle学习笔记----使用HQL语句实现复杂查询
- Castle ActiveRecord学习实践(5)继承
- Castle ActiveRecord学习实践(8):数据有效性的验证
- Castle ActiveRecord学习实践(4):实现One-Many关系的映射
- Castle ActiveRecord学习实践(8)HQL查询
- Castle ActiveRecord学习实践(2):构建配置信息
- C#.Net Castle ActiveRecord学习实践(1):快速入门指南
- C#.Net Castle ActiveRecord学习实践(8):数据有效性的验证
- Castle ActiveRecord学习实践(8):数据有效性的验证
- Castle ActiveRecord学习实践(5):实现Many–Many关系的映射
- C#.Net Castle ActiveRecord学习实践(2):构建配置信息