将这样修改
Hibernate.property:
hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N'
hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username robert
hibernate.connection.password a
hibernate.connection.url jdbc:oracle:thin:@888-88:1521:DBName
hibernate.connection.pool_size 1
hibernate.proxool.pool_alias pool1
hibernate.jdbc.batch_size 0
hibernate.jdbc.use_streams_for_binary true
hibernate.max_fetch_depth 1
hibernate.cache.use_query_cache true
hibernate.cache.provider_class net.sf.hibernate.cache.HashtableCacheProvider
【问题描述】在工作中,有一个比较复杂的feature使用的是hibernate的criteria实现的查询,但是pm要求在查询数据前,先告知用户有多少条数据,让用户去选择返回多少条记录。
【解决办法】使用projections.count(property) 或者 projections.rowcount()
criteria c = session.createcriteria(user.class.getname());
c.setprojection(projections.rowcount());
checkrownumresponsebean result = new checkrownumresponsebean();
result.setrownum(integer.parseint(c.uniqueresult().tostring()));
查看hibernate生成的query是:selectcount(*) as y0_fromuser this_where(1=1)备注:有些朋友可能使用的是c.list.size().返回的记录数,其实这样的话,hibernate是从数据库里面把所有满足条件的记录查询出来放到一个list里面,然后再用list的length返回长度。这样不是此需求的初衷。因为这样会消耗性能。。。并且在数据过多的情况下面,会造成tomcat的outofmemory错误
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....