迎接2007年散分200,顺便分享关于hibernate3查询的一点经验,有不对的地方,大家多提意见!

JIEK_ONE 2006-12-30 12:33:45
最近在用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>
就可以了。

...全文
361 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
夜的眼2021 2007-01-04
  • 打赏
  • 举报
回复
JF
wsb_8224 2007-01-03
  • 打赏
  • 举报
回复
新年快乐
crazycy 2007-01-02
  • 打赏
  • 举报
回复
mark
zzhzzh204553 2007-01-02
  • 打赏
  • 举报
回复
刚学,接分.
java_just 2007-01-02
  • 打赏
  • 举报
回复
jf
cloudtarget 2007-01-01
  • 打赏
  • 举报
回复
支持,Happy New Year! &jf
jiao38 2007-01-01
  • 打赏
  • 举报
回复
jf
ming4098 2006-12-31
  • 打赏
  • 举报
回复
呵呵,接分
laiseeme 2006-12-31
  • 打赏
  • 举报
回复
mark
谢谢
wsb_8224 2006-12-31
  • 打赏
  • 举报
回复
支持
wqvbka123 2006-12-31
  • 打赏
  • 举报
回复
看不懂,太长!
itjonney 2006-12-31
  • 打赏
  • 举报
回复
不错,不一定好用呀
syhan 2006-12-31
  • 打赏
  • 举报
回复
谢lz,来jf
miaoshengwu 2006-12-30
  • 打赏
  • 举报
回复
坐地上
imA 2006-12-30
  • 打赏
  • 举报
回复
抢地板
imA 2006-12-30
  • 打赏
  • 举报
回复
抢板凳
imA 2006-12-30
  • 打赏
  • 举报
回复
抢沙发
  • 打赏
  • 举报
回复
学习,接分
怡静如诗 2006-12-30
  • 打赏
  • 举报
回复
mark
wsb_8224 2006-12-30
  • 打赏
  • 举报
回复
学习
加载更多回复(10)

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧