您的位置:首页 > 其它

Hibernate Criteria查询

2014-02-21 16:05 225 查看


查询指定字段
Criteriacriteria=session.createCriteria(Person.class);
ProjectionListplist=Projections.projectionList();
plist.add(Projections.property("entity_column1"));
plist.add(Projections.property("entity_column2"));
plist.add(Projections.property("entity_column3"));
criteria.setProjection(plist);
List<Object[]>result=criteria.list();
//这样的结果集结构为List<Object[]>
//Object[0]=entity_column1value
//Object[1]=entity_column1value
//Object[2]=entity_column1value


分组

单列分组:

criteria.setProjection(Projections.groupProperty("entity_column"));

多列分组:

ProjectionListplist=Projections.projectionList();
plist.add(Projections.groupProperty("entity_column1"));
plist.add(Projections.groupProperty("entity_column2"));
plist.add(Projections.groupProperty("entity_column3"));
criteria.setProjection(plist);


计算总行数:

IntegertotalLines=(Integer)criteria.setProjection(Projections.rowCount()).uniqueResult();
IntegertotalLines=(Integer)criteria.setProjection(Projections.count("entity_column")).uniqueResult();
//1,首先需要在程序中可以拿到要count的几个列。可以在某个类中设置静态属性。
//2,创建类MyEmptyInterceptor,覆盖onPrepareStatement方法:
MyEmptyInterceptorextendsEmptyInterceptor{
onPrepareStatement(Stringsql){
//对sql进行处理。
//处理结果为:
selectcount(value(char(alias.table_column1),'')||value(char(alias.table_column2),'')||...)from...
//这里的函数为DB2函数,数据库不同使用不同的函数处理,value函数是指如果参数一为null,则使用''来标识,在DB2中'||'两边均不能为null和非字符串类型
}
}


//3,在sessionFactory的bean配置中,配置property
<beanid="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
<propertyname="entityInterceptor">
<beanid="myEmptyInterceptor"class="com.silone.dao.interceptor.MyEmptyInterceptor"></bean>
</property>
...
</bean>



//3,在sessionFactory的bean配置中,配置property
<beanid="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
<propertyname="entityInterceptor">
<beanid="myEmptyInterceptor"class="com.silone.dao.interceptor.MyEmptyInterceptor"></bean>
</property>
...
</bean>


原理:AnnotationSessionFactoryBean的超类LocalSessionFactoryBean有entityInterceptor属性,在hibernate将sql语句发送到数据库执行前

调用了entityInterceptor的onPrepareStatement方法。且参数为已经生成好的sql语句。所以可以在sessionFactory中将entityInterceptor

给覆盖掉,注入自己的interceptor。那么调用的是子类的方法,MyEmptyInterceptor的onPrepareStatement方法。原理:AnnotationSessionFactoryBean的超类LocalSessionFactoryBean有entityInterceptor属性,在hibernate将sql语句发送到数据库执行前

调用了entityInterceptor的onPrepareStatement方法。且参数为已经生成好的sql语句。所以可以在sessionFactory中将entityInterceptor

给覆盖掉,注入自己的interceptor。那么调用的是子类的方法,MyEmptyInterceptor的onPrepareStatement方法。



//3,在sessionFactory的bean配置中,配置property
<beanid="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
<propertyname="entityInterceptor">
<beanid="myEmptyInterceptor"class="com.silone.dao.interceptor.MyEmptyInterceptor"></bean>
</property>
...
</bean>




IntegertotalLines=criteria.setProjection(Projections.countDistinct("entity_column1")).uniqueResult();

这样只是对entity_column1去除了重复并统计行数。如果要统计多列不重复的数据。那么就要countDistinct多列。

那么借助count多列的做法,最终处理的结果为

selectcount(distinctvalue(char(alias.table_column1),'')||value(char(alias.table_column2),'')||...)from...

在第一列的前方使用distinct关键字。不同数据库不同做法。

去除重复数据:

1,使用groupby,要去除重复的数据都用groupby。

2,使用distinct关键字。

ProjectionListplist=Projections.projectionList();

plist.add(Projections.property("entity_column1"));

plist.add(Projections.property("entity_column2"));

plist.add(Projections.property("entity_column3"));

Projectionp=Projections.distinct(plist);

criteria.setProjection(p).list();

怎么排序?

criteria.addOrder(Order.desc("entity_column"));

如果数据库中有null,'',一般数据。那么排序如何排?

参数count多列的方案,使用Interceptor。

处理结果为

selectvalue(table_column1tc1,''),table_column2tc2,...from...orderbytc1

selecttable_column1tc1,value(table_column2,'')tc2,...from...orderbytc2

orderby哪个字段,那么这个字段就要嵌套value函数。不同数据库。不同函数



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: