转发:Using HQL (Hibernate Query language) http://www.castleproject.org/activerecord/documentation/v1rc1/usersguide/hql.html
来源:互联网 发布:淘宝客分佣cms系统 编辑:程序博客网 时间:2024/06/05 07:03
SimpleQuery and ScalarQuery
SimpleQuery and ScalarQuery can be used in cases where the query would be a direct HQL query call.
Here are some examples:
- Using generics
- Not using generics
[ActiveRecord]public class Blog : ActiveRecordBase<Blog>{ ... // Static method from Blog class, retrieves all Post instances // from the blog of the specified author. Uses positional parameters. public static Post[] GetPostsFromAuthor( String author ) { SimpleQuery<Post> q = new SimpleQuery<Post>(@" from Post p where p.Blog.Author = ? ", author); return q.Execute(); } // Static method from Blog class, retrieves the ID of all Post instances // in a specified date interval. Uses named parameters. public static int[] GetPostIdsFromInterval( DateTime start, DateTime end ) { // the second parameter specifies the AR type used for determining the database connection SimpleQuery<int> q = new SimpleQuery<int>(typeof(Post), @" select p.ID from Post p where p.Date between :start and :end ");
q.SetParameter("start", start);
q.SetParameter("end", end);
return q.Execute(); } // Instance method from Blog, gets the last post date. public DateTime LastPostDate() { ScalarQuery<DateTime> q = new ScalarQuery<DateTime>(typeof(Post), @" select max(p.Date) from Post p where p.Blog = ? ", this); return q.Execute(); }}
Custom Query
If your want:
- encapsulation of your business rules within a query object;
- custom, advanced parameter handling;
- conditional queries (i.e., building the HQL and parameters manually);
- direct use of NHibernate's
Criteria
; or - direct access to NHibernate's
ISession
orIQuery
objects
Then you can write a custom ActiveRecord query.
You just need to inherit from ActiveRecordBaseQuery (or implement the IActiveRecordQuery interface).
For example:
public class MyCustomQuery : ActiveRecordBaseQuery{ private String authorName = null; private int maxResults = 2; public MyCustomQuery() : base(typeof(Blog)) { } public MyCustomQuery(string authorName) : base(typeof(Blog)) { this.AuthorName = authorName; } public int MaxResults { get { return this.maxResults; } set { this.maxResults = value; } } public String AuthorName { get { return this.authorName; } set { this.authorName = value; } } protected override void CreateQuery(ISession session) { String hql = "from Blog b"; if (this.AuthorName != null) hql += " where b.Author like :author"; IQuery q = session.CreateQuery(hql); if (AuthorName != null) q.SetString("authorName", this.AuthorName); q.SetMaxResults(this.MaxResults); return q; } protected override object InternalExecute(ISession session) { IQuery q = CreateQuery(session); return SupportingUtils.BuildArray(typeof(Blog), q.List(), null, false); }}
The usage:
[ActiveRecord]public class Blog : ActiveRecordBase{ ... public Blog[] GetThreeBlogsFromAuthor( String authorName ) { QueryWithNamedParameters q = new QueryWithNamedParameters(); q.AuthorName = authorName; q.MaxResults = 3; return (Blog[]) ExecuteQuery(q); }}
Execute Callback
The third way to write custom queries is to use the ActiveRecordBase.Execute method, which basically does the same as the Custom Query approach, but without the need to write any additional classes.
ActiveRecord]
public class Blog : ActiveRecordBase<Blog>
{
...
public static Post[] GetPostsFromAuthor( String author )
{
return (Post[]) Execute(
delegate(ISession session, object instance)
{
// create the query...
IQuery query = session.CreateQuery( "from Post p where p.Blog.Author = :author" );
// set the parameters...
query.SetString("author", (String) instance);
// fetch the results...
IList results = query.List();
// OPTIONAL: convert the results to an array or
// something meaningful, instead of returning the IList
Post[] posts = new Post[results.Count];
results.CopyTo(posts, 0);
// return
return posts;
}, author);
}
}
public class Blog : ActiveRecordBase<Blog>
{
...
public static Post[] GetPostsFromAuthor( String author )
{
return (Post[]) Execute(
delegate(ISession session, object instance)
{
// create the query...
IQuery query = session.CreateQuery( "from Post p where p.Blog.Author = :author" );
// set the parameters...
query.SetString("author", (String) instance);
// fetch the results...
IList results = query.List();
// OPTIONAL: convert the results to an array or
// something meaningful, instead of returning the IList
Post[] posts = new Post[results.Count];
results.CopyTo(posts, 0);
// return
return posts;
}, author);
}
}
- 转发:Using HQL (Hibernate Query language) http://www.castleproject.org/activerecord/documentation/v1rc1/usersguide/hql.html
- HQL(Hibernate Query Language)
- Hibernate Query Language (HQL)
- HQL(Hibernate Query Language)
- Hibernate Query Language(HQL)
- About Hibernate Query Language(HQL)
- HQL(Hibernate Query Language)
- Hibernate查询语言(Query Language), 即HQL
- HQL(Hibernate Query Language)使用详解
- hibernate-HQL(query language)-16
- hibernate参考手册之---Hibernate查询语言(Query Language), 即HQL
- Hibernate学习笔记-HQL(Hibernate Query Language)
- Hibernate学习笔记-HQL(Hibernate Query Language)
- Hibernate检索方式之HQL(Hibernate Query Language)
- HQL:Hibernate Qusery Language
- Hibernate查询(Query Language,hql,ejbql,nativeql,qbc,qbe)
- 关于HQL(Hibernate Query Language)的查询方法
- Hibernate Query examples (HQL)
- WINDOWS -> 端口详解及如何开起端口关闭端口 [打印本页]
- SQL Server2005通用分页存储过程
- delphi语法基础--过程
- Oracle的左连接和右连接
- 在iMac上安装Tiger,在DMG方式安装Leopard
- 转发:Using HQL (Hibernate Query language) http://www.castleproject.org/activerecord/documentation/v1rc1/usersguide/hql.html
- C# 创建文件并往文件里写内容
- Bugzilla简明使用手则
- Struts中配置Action
- Java/JSP中使用JDBC连接SQL Server 2005
- js动态取页面元素位置的方法
- ext-2.0扩展多选下拉框(代码及演示)
- 调用批处理
- 用Rysnc同步服务器文件