本站首页    管理页面    写新日志    退出


«November 2025»
1
2345678
9101112131415
16171819202122
23242526272829
30


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7658692
建立时间: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;    


阅读全文(1915) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.865 second(s), page refreshed 144814583 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号