免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1364 | 回复: 0
打印 上一主题 下一主题

hibernate里面实现复杂的查询 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-21 15:35 |只看该作者 |倒序浏览

由于hibernate对sql的二次封装,使部分复杂的查询语句不能被执行,我列举两种解决方法,和大家讨论!

第一种:查询结果集
 Session session = this.getSession();
        List result = new ArrayList();
        String strSql = "select buy.isbn,buy.bookname,buy.bookengname,count(*) as counum from (select distinct usee.isbn 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.isbn= ddd.isbn and usee.isbn= buy.isbn group by buy.isbn,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("isbn"));
                    tempList.add(rs.getString("bookname"));
                    tempList.add(rs.getString("bookengname"));
                    tempList.add(rs.getString("counum"));
                    result.add(tempList);
                    rs.next();
                }
              }
        }catch (SQLException e)
        {
            System.out.println("----" + e);
        }
        catch (HibernateException e)
        {
            System.out.println("----" + e);
        }
        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)
        {
            System.out.println("HibernateException" + e);
        }
        finally
        {
            this.closeSessionIfNecessary(session);
        }
        return aCount;

这两种方法实际上都是使用hibernate的session实现!


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/5142/showart_16880.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP