您的位置:首页 > 其它

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

2014-10-17 16:45 393 查看
早期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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: