在注册的时候需要判断其中2个字段是否在数据库中存在
userinfoImpl:public List findByProperty(String propertyName, Object value) {
Session session = HibernateSessionFactory.getSessionFactory()
.getCurrentSession();
Query queryObject = null;
try {
session.beginTransaction();
String queryString = "from Userinfo as model where model."
+ propertyName + " = ? ";
queryObject = session.createQuery(queryString);
queryObject.setParameter(0, value);
System.out.println("session is closed: "
+ queryObject.list().size());
session.getTransaction().commit();
return queryObject.list();
} catch (RuntimeException re) {
log.error("UserinfoServicesImpl  findByProperty failed", re);
}
return null;
}
hibernate.cfg.xml
<property name="current_session_context_class">thread</property>Action:
List useridList = userinfoImpl.findByProperty("userid", userid);控制台输出:
session is closed: 1
org.hibernate.SessionException: Session is closed!这个该如何解决?

解决方案 »

  1.   

    getCurrentSession();
    这行是得到当前已经存在的session,在你贴出来的代码来看,它要么为空,要么是关闭的。
    你改成openSession();就行了。
      

  2.   

    用了openSession确实是好了,那我还有个问题想问下
    在网上看了一些资料,
    在获取session 的时候,有的是
    Session session = HibernateSessionFactory.getSessionFactory() .getCurrentSession();
    有的是
    Session session = HibernateSessionFactory.getSessionFactory() .getCurrentSession();
    还有的是
    Session session = HibernateSessionFactory.getSession();如果项目的框架是Struts+Hibernate 3.2 的话,改选用哪种方法来获取Session?
      

  3.   

    用他自带的getsession就可以创建session啦。他自己有个方法就叫getsession。
      

  4.   

    继承  HibernateDaoSupport 用他自带的getsession就可以创建session
      

  5.   


    兄弟HibernateDaoSupport  是Spring的。
      

  6.   

    System.out.println("session is closed: "
                        + queryObject.list().size());
                session.getTransaction().commit();
                return queryObject.list();我觉得代码可能有点问题, 没环境看。 session commit之后使用getCurrentSession获取到的session会被自动关闭的, 之后你queryObject.list session应该是关了的。 确认一下?