一、多表查询分页
方法一:
public List getUserlist(String userAccount,String EmployeeFullName,
int startNum,int pagelength){
int totalResults; //总记录的条数
String condition="";
if(!userAccount.equals("")&;&;!EmployeeFullName.equals(""))
{
condition=" and sysuser.susUseraccount
like'%"+userAccount+"%' or employee.coeFullname
like'%"+EmployeeFullName+"%'";
}
else if(!EmployeeFullName.equals("")&;&;userAccount.equals(""))
{
condition=" and employee.coeFullname
like'%"+EmployeeFullName+"%'";
}
else if(!userAccount.equals("")&;&;EmployeeFullName.equals(""))
{
condition=" and sysuser.susUseraccount
like'%"+userAccount+"%'";
}
String hql ="select
employee.coeEmployeeid,employee.coeFullname,employee.coeSexuality,employee.coePictureurl,sysuser.susAccountid, sysuser.susUseraccount,
sysuser.susForumUsersLevel from SmcSystemuser sysuser,HrmEmployee
employee where sysuser.susEmployeeid=employee.coeEmployeeid
"+condition+" order by sysuser.susAccountid";
System.out.println("hql:"+hql);
List res = new Vector();// 将用于存放保存的结果集合
Session session = null;
ScrollableResults srs = null;
try
{
//得到一个Hibernate Session
//分页关键语句
session = this.getSessionFactory().openSession();
//下面创建一个匿名Query实例并调用它的scroll()方法返回以
//ScrollableResults形式组织的
查询结果
Query query = session.createQuery(hql);
query.setFirstResult(startNum);
query.setMaxResults(pagelength);
srs = query.scroll();
// 将查询结果放入List保存
int i=0;
while(srs.next())
{ i++;
res.add(new EmpUsrAccountBean(srs.getString(0),
srs.getString(1),
srs.getString(2),
srs.getString(3),
srs.getString(4),
srs.getString(5),
srs.getString(6)));
}
}
catch(HibernateException he)
{
if(srs!=null)
{
try
{
srs.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println(he.getMessage());
}
finally
{
try
{
session.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
return res;
}
错误:在执行到srs = query.scroll(); 报如下错误 ERROR [AssertionFailure] an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session) org.hibernate.AssertionFailure: scrollable result sets are not enabled at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatch er.java:413) 原因分析:jboss里没有连结数据库的驱动
解决办法:在jboss目录下的\server\default\lib中加入数据库驱动
方法二:
将获取的值存入一个新的list对象中,在页面中list[](list数组)取值
public List getEmplistByName(String empName,int offset,int length) { List emplist=new ArrayList(); String hql=""; try { if(empName.equals("")) { hql="select new list(emp,org) from HrmEmployee emp,HrmOrganizations org where emp.coeBelongdepid=org.cooOrganizationid order by emp.coeEmployeeid"; }else { hql="select new list(emp,org) from HrmEmployee emp,HrmOrganizations org where emp.coeBelongdepid=org.cooOrganizationid and emp.coeFullname like'%"+empName+"%' order by emp.coeEmployeeid"; } return getListForPage(hql, offset,length) ; } catch (DataAccessException e) { e.printStackTrace(); return emplist; } } 页面中取值为
${emplist[0].coeFullname} | 二、配置JDK路径和名称
右键工程->properties->
Java Build Path->Libraries->选中JRE System Libraries,点击按钮 remove掉,然后单击Add Libraries->选中JRE System Libraries->配置新的JDK路径和名称, 确定即可