2、ICriteria,在例如产品分类这种树结构的表中,通常一个分类有它的子分类(集合),和他的父分类,一般我们查一级分类的条件是,那些父分类是空的分类:
public IList<Category> GetRootCategories()
{
ICriteria crit = Session.CreateCriteria(this.PersistentClass);
crit.Add(Expression.Eq("Parent",null));
return crit.List<Category>();
}
其中"Parent"是那个分类的父类这个Parent是对于类的属性而不是数据库字段,但是这样却查不到结果Nhibernate 产生如下的sql:"
exec sp_executesql N'SELECT this_.[s_id] as s1_1_0_, this_.[s_show] as s2_1_0_, this_.[s_order] as s3_1_0_, this_.[s_type] as s4_1_0_ FROM product_sort this_ WHERE this_.[s_type] = @p0 and this_.[s_show] = @p1', [email=N'@p0]N[/email] bigint,@p1 bit', @p0 = NULL, @p1 = 1"
其中s_type就是对应了Parent但是这样是错误的正确的应该是产生如下的SQL:
exec sp_executesql N'SELECT this_.[s_id] as s1_1_0_, this_.[s_show] as s2_1_0_, this_.[s_order] as s3_1_0_, this_.[s_type] as s4_1_0_ FROM product_sort this_ WHERE this_.[s_type] is null and this_.[s_show] = @p1', [email=N'@p1]N[/email] bit', @p1 = 1
但是我们不可能因为这个而去修改它的源代码(哈哈其实这是典型99%对hibernate的使用不当范围内),只要改成
public IList<Category> GetRootCategories()
{
ICriteria crit = Session.CreateCriteria(this.PersistentClass);
crit.Add(new NullExpression("Parent"));
return crit.List<Category>();
}
就可以了,new NullExpression("Parent")会自动产生(is null)的sql。
分享到:
相关推荐
c#编写无SQL程序,ORM自动映射,NHibernate-5.2.4-bin.zip ,里面代码完全,亲测好用,与大家共享
NHibernate-2.0.0.CR1-bin.zip
NHibernate-3.3.2.GA-bin 下载
nhibernate-core-3.3.1GA-207-gcd6e862 源码
NHibernate-2.1.0.GA-bin NHibernate-2.1.0.GA-bin NHibernate-2.1.0.GA-bin NHibernate-2.1.0.GA-bin
使用与.net开发的NHibernate框架,这个版本是比较老,但是比较经典的。 我也是使用这个来在.net开发的。给大家共享一下。
NHibernate 引用NHibernate的需要的DLL
NHibernate-2.0.1NHibernate-2.0.1NHibernate-2.0.1NHibernate-2.0.1NHibernate-2.0.1NHibernate-2.0.1NHibernate-2.0.1NHibernate-2.0.1
nhibernate-core-3.2.0GA
Nhibernate 3.0.0 需要的所有东西
NHibernate-3.2的源码,想了解的同学可以下载来看
NHibernate-3.3.3.SP1-reference(201308)
NHibernate-3.2.0.GA-bin 安装包
最新的NHibernate-4.0.1.GA-bin
NHibernate-4.1.1.GA-bin
NHibernate-5.3.9
NHibernate-1.2.0.GA-bin.zip
经典的《nhibernate-in-action》一书的源代码,从Manning网站上直接下载下来的,不容易,因为这个网站从国内是上不去的。希望对大家学习有帮助
NHibernate-2.0.1.pdf 资料,希望大家喜欢!
NHibernate是Hibernate 的 .NET Framework 实现版本,NHibernate-2.0.1.GA是目前最新版本