关于hibernate混合使用占位符和命名参数

时间:2014-10-17 18:59:05   收藏:0   阅读:471

早期hibernate不支持jdbc风格的占位符和命名参数两种方式混合使用

但是新版本的hibernate是可以支持的,


    @Test
    public void testMixParameterMethod() {
        //String hqlString ="from Org org where org.address = ? and org.code = ? ";
        //String hqlString ="from Org org where org.address = :address and org.code in (:codes) ";
        String hqlString ="from Org org where org.address = ? and org.code in (:codes) ";
        Query queryObject = getSession().createQuery(hqlString);
        queryObject.setParameter(0, "海淀");    //占位符方式
        //queryObject.setParameter(1, 102);
        //queryObject.setParameter("address", "海淀");
        queryObject.setParameterList("codes", new Integer[]{102, 103, 104});    //命名参数方式
        List<?> list = queryObject.list();
        for (int i=0; i<list.size(); i++) {
            Org org = (Org)list.get(i);
            System.out.println(i + "---" + org.getId());
        }
        
        System.out.println("-----------------testMixParameterMethod ok------------------------");

    }

本文出自 “robinc” 博客,请务必保留此出处http://robinc.blog.51cto.com/439699/1565211

原文:http://robinc.blog.51cto.com/439699/1565211

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!