在注册的时候需要判断其中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!这个该如何解决?
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!这个该如何解决?
解决方案 »
- java手机web开发
- freemarker模板+hibernate分页问题,各位前辈帮帮忙啊
- Struts2的Annotation问题?请帮忙呀!
- 做例子时遇到的问题
- 一个挑战自我的排序问题!请看看!!!
- 用struts 做下拉框的连动怎样做
- logic:iterate struts 标签使用问题?
- 求助:关于struts标签里的optionsCollection错误
- 请教在JAVA中使用数据库!
- webservice 出错(鸡毛帖),散分
- jbpm3.1.2 新增任务实例时 JBPM_TASKINSTANCE.ACTORID为空,望老大门来看一眼
- 页面校验你是选择手写JS还是commons-validator之类的
这行是得到当前已经存在的session,在你贴出来的代码来看,它要么为空,要么是关闭的。
你改成openSession();就行了。
在网上看了一些资料,
在获取session 的时候,有的是
Session session = HibernateSessionFactory.getSessionFactory() .getCurrentSession();
有的是
Session session = HibernateSessionFactory.getSessionFactory() .getCurrentSession();
还有的是
Session session = HibernateSessionFactory.getSession();如果项目的框架是Struts+Hibernate 3.2 的话,改选用哪种方法来获取Session?
兄弟HibernateDaoSupport 是Spring的。
+ queryObject.list().size());
session.getTransaction().commit();
return queryObject.list();我觉得代码可能有点问题, 没环境看。 session commit之后使用getCurrentSession获取到的session会被自动关闭的, 之后你queryObject.list session应该是关了的。 确认一下?