精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-04-08
跟Hibernate(十五):用Hibernate求记录总数的一个怪胎
里描述的问题相关. DetachedCriteria hasInfoForCurrentDeptQuery = DetachedCriteria.forClass(ContractTrade.class); hasInfoForCurrentDeptQuery.setProjection(Projections.projectionList() .add(Projections.rowCount(), "rowCount")); //设置子查询 DetachedCriteria hasImportedQuery = DetachedCriteria.forClass(ContractOtherLink.class); hasImportedQuery.setProjection(Property.forName("linkContId")); hasInfoForCurrentDeptQuery.add(Property.forName("contractTradeId").notIn(hasImportedQuery)); hasInfoForCurrentDeptQuery.setResultTransformer(DetachedCriteria.ALIAS_TO_ENTITY_MAP); Map result = (Map)getHibernateTemplate() .findByCriteria(hasInfoForCurrentDeptQuery).get(0); // --(1) 这样的代码执行在(1)处时整个程序卡在那不走了.怀疑是DetachedCriteria子查询与DetachedCriteria.ALIAS_TO_ENTITY_MAP配合上出了问题, 于是看Hibernate生成的SQL(生成sql: select count(*) as y0_
from UPLOAD_CONTRACT_ITRADE_VIEW this_ where this_.DEPT_ID=24 and this_.CONTRACT_ID not in (select this0__.LINK_CONT_ID as y0_
from CONTRACT_OTHER_LINK this0__)
), 再在Toad里执行是,没问题. 出结果了.
Session session = sessionFactory.getCurrentSession(); Transaction t = session.beginTransaction(); DetachedCriteria queryCirteriaOuter = DetachedCriteria.forClass(DataDict.class); queryCirteriaOuter.setProjection(Projections.projectionList() .add(Projections.rowCount(), "rowCount")); DetachedCriteria queryCirteriaInner= DetachedCriteria.forClass(DataDictLink.class); queryCirteriaInner.add(Restrictions.like("tableName", "S_CONTACT")); queryCirteriaInner.setProjection(Property.forName("columnName")); queryCirteriaOuter.add(Property.forName("listType").in(queryCirteriaInner)); queryCirteriaOuter.setResultTransformer(DetachedCriteria.ALIAS_TO_ENTITY_MAP); Criteria executableCriteria = queryCirteriaOuter.getExecutableCriteria(session); Map map = (Map) executableCriteria.list().get(0); System.out.println("result: "+map.get("rowCount")); t.commit(); 没问题, 结果很好地显示了出来.
SessionFactory sessionFactory = HiberUtil.getSessionFactoryOracle(); Session session = sessionFactory.getCurrentSession(); Transaction t = session.beginTransaction(); Connection conn = session.connection(); String sql = "select count(*) as y0_ from UPLOAD_CONTRACT_ITRADE_VIEW this_ "+ "where this_.DEPT_ID=24 and this_.CONTRACT_ID "+ "not in (select this0__.LINK_CONT_ID as y0_ from CONTRACT_OTHER_LINK this0__)"; PreparedStatement st = conn.prepareStatement(sql); ResultSet rs = st.executeQuery(sql); rs.next(); int result = rs.getInt("y0_"); System.out.println("result: "+ result); t.commit(); 还是没问题. ------------------------------------ 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-04-08
hasImportedQuery.setProjection(Property.forName("linkContId"));
改成 hasImportedQuery.setProjection(Property.forName("linkContId").as("linkContId")); 试试 |
|
返回顶楼 | |
发表时间:2009-04-08
daquan198163 写道 hasImportedQuery.setProjection(Property.forName("linkContId")); 改成hasImportedQuery.setProjection(Property.forName("linkContId").as("linkContId")); 试试 谢谢daquan198163的建议, 但还是不行. 还是卡在那一行就没反应了. |
|
返回顶楼 | |
发表时间:2009-04-09
我的hibernate版本是3.2.1,没有出现卡住的问题
|
|
返回顶楼 | |
浏览 4329 次