迎接2007年散分200,顺便分享关于hibernate3查询的一点经验,有不对的地方,大家多提意见!
最近在用hibernate3做项目的过程中,用这样的一个地方用户输入开始日期,结束日期提交得到满足条件的记录并分页显示。
处理的代码如下,大家看看我的代码有什么地方设计的不好的,请多多批评:
/**
* @author 罗洁
* @version 1.0 2006-12-30
*/
public class IntroduceFundProvinceOuterList_Fun {
//初始化seesion 在此类里所有方法用这个seesion这样的目的是为了不需要对每个方法实列一个seeions,提高资源的利用效率
Session session = util.HibernateUtil.currentSession();
Transaction transaction = null;
//查询的变量定义
static String s_StartDate="1900-12-31";
static String s_EndDate="2007-12-31";
static int page = 1;
static int numbers = 20;
/**
* 进行分页查询
* @param s_StartDate 开始日期
* @param s_EndDate 结束日期
* @param page 当前页
* @param numbers 每页返回记录数条数
* 使用session.createQuery().iterate()充分利用缓存
*/
public Iterator getIntroduceFundProvinceOuterListInfo(int page,int numbers,String s_StartDate, String s_EndDate){
String hql ="from EintroduceFundProvinceOuterList";
Iterator it = null;
try{
it = session.createQuery(hql).setFirstResult(
(page - 1) * numbers).setMaxResults(numbers).iterate();
}catch(Exception e){
e.printStackTrace();
}
return it;
}
/**
* 有多少条满足条件的数据
* @param s_StartDate 开始日期
* @param s_EndDate 结束日期
*/
public int getIntroduceFundProvinceOuterListTotal(String s_StartDate, String s_EndDate){
//String hql ="select distinct iFPOLInfo from EintroduceFundProvinceOuterList iFPOLInfo";
String hql ="from EintroduceFundProvinceOuterList";
Iterator it = null;
int resultTotal = 0;
try{
it = session.createQuery(hql).iterate();
}catch(Exception e){
e.printStackTrace();
}
while(null!=it&&it.hasNext()){
resultTotal++;
}
return resultTotal;
}
/**
* @关闭seesion
*/
public void sessionClose() {
HibernateUtil.closeSession();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("开始查询!");
Iterator it = null;
EintroduceFundProvinceOuterList eintroduceFundProvinceOuterList = new EintroduceFundProvinceOuterList();
IntroduceFundProvinceOuterList_Fun introduceFundProvinceOuterList_Fun = new IntroduceFundProvinceOuterList_Fun();
it = introduceFundProvinceOuterList_Fun.getIntroduceFundProvinceOuterListInfo(page,numbers,s_StartDate, s_EndDate);
System.out.println("满足条件的总数:"+introduceFundProvinceOuterList_Fun.getIntroduceFundProvinceOuterListTotal(s_StartDate, s_EndDate));
try{
if(null != it){
while(it.hasNext()){
eintroduceFundProvinceOuterList = (EintroduceFundProvinceOuterList)it.next();
if(null!=eintroduceFundProvinceOuterList)
System.out.println("TableDate="+eintroduceFundProvinceOuterList.getTableDate());
}
}
System.out.println("结束查询!");
}catch(Exception e){
e.printStackTrace();
}finally{
introduceFundProvinceOuterList_Fun.sessionClose();
}
}
}
在main方法里面我执行getIntroduceFundProvinceOuterListInfo和getIntroduceFundProvinceOuterListTotal方法,出现异常如下:
1:org.hibernate.exception.JDBCConnectionException: could not execute query
2:Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
当我只执行其中一个时候就没有出现异常,找了一些资料,发现出现这样的情况是因为:这个错误产生的原因一般是当你在一个SQL SERVER的JDBC连接上执行多个STATEMENTS的操作,或者是手动事务状态(AutoCommit=false) 并且使用 direct (SelectMethod=direct) 模式. Direct 模式是默认的模式.
找到问题的原因,现在把hibernate.cfg.xml里面的
<property name="hibernate.connection.url">
jdbc:microsoft:sqlserver://192.168.1.11:1433;databasename=EconomyInfo
</property>改为
<property name="hibernate.connection.url">
jdbc:microsoft:sqlserver://192.168.1.11:1433;databasename=EconomyInfo;SelectMethod=cursor
</property>
就可以了。