流程:
读取action然后把List的数据显示到页面上
public List findAllCardinfo(String userid, Long cardid) {
Session session = HibernateSessionFactory.getSession();
StringBuffer sb = new StringBuffer();
Transaction tx = null;
Query queryObject = null;
try {
tx = session.beginTransaction();
sb
.append("select new com.telezone.vo.CardinfoVO( cinfo.cardid ,cinfo.cardname , oinfo.ownername ,uinfo.username , cinfo.phonenum,cinfo.cardstate,cinfo.createtime,cinfo.otherinfo ) from Cardinfo as cinfo left join cinfo.cardownerinfo as oinfo left join cinfo.userinfo as uinfo where uinfo.userid = :uid ");
// Long 类型 cardid 默认取值 0
if (cardid > 0)
sb.append(" and cinfo.cardid = :cid");
sb.append(" order by cinfo.cardid"); queryObject = session.createQuery(sb.toString());
queryObject.setParameter("uid", userid);
if (cardid > 0)
queryObject.setParameter("cid", cardid);
tx.commit();
} catch (RuntimeException re) {
tx.rollback();
log.error("==CardinfoServices findAllCardinfo failed==", re);
re.printStackTrace();
} finally {
session.close();
}
return queryObject.list();
}
把session.close()注释掉就可以了但是session不是用完就需要关闭的吗?
用的是Struts+Hibernate
读取action然后把List的数据显示到页面上
public List findAllCardinfo(String userid, Long cardid) {
Session session = HibernateSessionFactory.getSession();
StringBuffer sb = new StringBuffer();
Transaction tx = null;
Query queryObject = null;
try {
tx = session.beginTransaction();
sb
.append("select new com.telezone.vo.CardinfoVO( cinfo.cardid ,cinfo.cardname , oinfo.ownername ,uinfo.username , cinfo.phonenum,cinfo.cardstate,cinfo.createtime,cinfo.otherinfo ) from Cardinfo as cinfo left join cinfo.cardownerinfo as oinfo left join cinfo.userinfo as uinfo where uinfo.userid = :uid ");
// Long 类型 cardid 默认取值 0
if (cardid > 0)
sb.append(" and cinfo.cardid = :cid");
sb.append(" order by cinfo.cardid"); queryObject = session.createQuery(sb.toString());
queryObject.setParameter("uid", userid);
if (cardid > 0)
queryObject.setParameter("cid", cardid);
tx.commit();
} catch (RuntimeException re) {
tx.rollback();
log.error("==CardinfoServices findAllCardinfo failed==", re);
re.printStackTrace();
} finally {
session.close();
}
return queryObject.list();
}
把session.close()注释掉就可以了但是session不是用完就需要关闭的吗?
用的是Struts+Hibernate
解决方案 »
- springmvc Json Jquery easyui 传值问题
- weblogic.application.ModuleException:(急急)
- struts2 Ajax验证用户名是出现异常,望各位帮帮忙!谢谢
- [Fatal Error] :1:8: The processing instruction target matching "[xX][mM][lL]" is not allowed.
- 关于hibernate一个很简单很简单的问题(-:
- 请问现在用struts开发网站时,页面是直接一行一行手写,还是先写好html代码再改,还是有特别的工具直接调用标签库写页面??????
- 如何引入自定义Package
- 广州,新成立不久的公司,需j2ee方面人才,有意入内看看
- hibernate连接数据库出错
- 我在deploy时候报了一这个错误,可是我找不到是哪里出错了
- org.hibernate.LazyInitializationException: could not initialize proxy - the owni
- 这网站怎么样。提个建议都有分
要么把return放在TRY里面,要么在前面定义一个list,然后在try里面list=queryObject.list();才能返回.
我试试看
<filter>
<filter-name>openSession</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Session session = HibernateSessionFactory.getSession();
StringBuffer sb = new StringBuffer();
Transaction tx = null;
Query queryObject = null;
List rlist=null;
try {
tx = session.beginTransaction();
sb
.append("select new com.telezone.vo.CardinfoVO( cinfo.cardid ,cinfo.cardname , oinfo.ownername ,uinfo.username , cinfo.phonenum,cinfo.cardstate,cinfo.createtime,cinfo.otherinfo ) from Cardinfo as cinfo left join cinfo.cardownerinfo as oinfo left join cinfo.userinfo as uinfo where uinfo.userid = :uid ");
// Long 类型 cardid 默认取值 0
if (cardid > 0)
sb.append(" and cinfo.cardid = :cid");
sb.append(" order by cinfo.cardid"); queryObject = session.createQuery(sb.toString());
queryObject.setParameter("uid", userid);
if (cardid > 0)
queryObject.setParameter("cid", cardid);
rlist=queryObject.list();
tx.commit();
} catch (RuntimeException re) {
tx.rollback();
log.error("==CardinfoServices findAllCardinfo failed==", re);
re.printStackTrace();
} finally {
session.close();
}
return rlist;
}
tx = session.beginTransaction();
sb
.append("select new com.telezone.vo.CardinfoVO( cinfo.cardid ,cinfo.cardname , oinfo.ownername ,uinfo.username , cinfo.phonenum,cinfo.cardstate,cinfo.createtime,cinfo.otherinfo ) from Cardinfo as cinfo left join cinfo.cardownerinfo as oinfo left join cinfo.userinfo as uinfo where uinfo.userid = :uid ");
// Long 类型 cardid 默认取值 0
if (cardid > 0)
sb.append(" and cinfo.cardid = :cid");
sb.append(" order by cinfo.cardid"); queryObject = session.createQuery(sb.toString());
queryObject.setParameter("uid", userid);
if (cardid > 0)
queryObject.setParameter("cid", cardid);
return queryObject.list(); tx.commit();
} catch (RuntimeException re) {
tx.rollback();
log.error("==CardinfoServices findAllCardinfo failed==", re);
re.printStackTrace();
} finally {
session.close();
}
}
建议楼主结合Spring 进行配置 或者自己写个过滤器 使用 ThreadLocal 完成 OpenSessionInView