hibernate 一对多查询 对多的一方进行分页
//查询用户留言
@Override
public List<LeaveWords> getLeaveWords(String userName) {
Session session = null;
List<LeaveWords> leaveWordsArray = new ArrayList<LeaveWords>();
try{
session = super.getHibernateSessionFactory().getSession();
String hql = "from UserInfo where userName=:userName";
Query query = session.createQuery(hql);
query.setString("userName", userName);
UserInfo userInfo = (UserInfo) query.list().get(0);
Hibernate.initialize(userInfo.getLeaveWords1());
leaveWordsArray.addAll(userInfo.getLeaveWords1());
} catch(HibernateException e) {
throw e;
}finally {
if(session != null) {
session.close();
}
}
return leaveWordsArray;
}
这个方法是查询用户留言的信息,通过 Hibernate.initialize(userInfo.getLeaveWords1()); 可以获得用户的留言信息。
但要对留言进行分页查询,这里该如何弄?
===================================回复========================================
多方的LIST做分页,HIBERNATE有专门的函数对LIST进行分页的。还可以进行排序
session.createFilter(userInfo.getLeaveWords1(),"order by xx").setFirstResult(10).setMaxResult(50).list();
具体这个createFilter的参数有何作用,请参考API