| Blog信息 |
|
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7659318 建立时间:2006年5月29日 |

| |
|
[Hibernate]Hibernate里面实现复杂的查询 软件技术, 电脑与网络
lhwork 发表于 2006/6/28 11:17:07 |
|
由于hibernate对sql的二次封装,使部分复杂的查询语句不能被执行,可以用hibernate的session实现!第一种:查询结果集 Session session = this.getSession(); List result = new ArrayList(); String strSql = "select buy.i***n,buy.bookname,buy.bookengname,count(*) as counum "; strSql = strSql + " from (select distinct usee.i***n from StuBasicInfo stu,Teachingmaterialuse usee ";
strSql = strSql + " where stu.identityid = usee.identityid and
stu.deptid = '" + banji + "' and usee.academicyearcode='" + xn + "' and
usee.semestercode = '" + xq + "') ddd,Teachingmaterialbuyinto buy,Teachingmaterialuse usee "; strSql = strSql + "where usee.i***n= ddd.i***n and usee.i***n= buy.i***n group by buy.i***n,buy.bookname,buy.bookengname"; System.out.println(strSql); try { Connection con = session.connection(); Statement statement = con.createStatement(ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = statement.executeQuery(strSql); if (rs.next()) { while (!rs.isAfterLast()) { List tempList = new ArrayList(); tempList.add(rs.getString("i***n")); tempList.add(rs.getString("bookname")); tempList.add(rs.getString("bookengname")); tempList.add(rs.getString("counum")); result.add(tempList); rs.next(); } } } catch (SQLException e) { e.printStackTrace(); } catch (HibernateException e) { e.printStackTrace(); } finally { this.closeSessionIfNecessary(session); }第二种:使用session Session session = this.getSession(); int aCount = 0; try { String sqlText = "select count(*) from Classroominfo room where room.flag='1' and room.type='" + userType + "' "; if ( (name != null) && ( (name != "null"))) { sqlText = sqlText + " and room.name like '%" + name + "%' "; } else if ( (buildingCode != null) && ( (buildingCode != "null"))) { sqlText = sqlText + " and room.buildingcode like '%" + buildingCode + "%' "; } else if ( (dept != null) && ( (dept != "null"))) { sqlText = sqlText + " and room.deptid ='" + dept + "' "; } aCount = ( (Integer) session.iterate(sqlText).next()).intValue(); } catch (HibernateException e) { e.printStackTrace(); } finally { this.closeSessionIfNecessary(session); } return aCount; |
|
|