無參存儲過程調用:
List<YourEntity> list = SessionFactory.getCurrentSession.createSQLQuery("{call procedure()}").addEntity(YourEntity.class).list();
有參數存儲過程調用:
List<YourEntity> list =
((SQLQuery) this.getCurrentSession().createSQLQuery("{call procedure(?)}").setString(0, yourParam))
.addEntity(YourEntity.class).list();
備註:這裏的存儲過程是選擇一個表的全部字段(即select * from table [where ...]),若是用用select a,b,c,... from table則程序在調用存儲過程後應加上.addScalar(
"a"
,Hibernate.STRING)
//列名
.addScalar(
"b"
,Hibernate.STRING)...綁定列名
.addScalar(
"b"
,Hibernate.STRING)...綁定列名