Exception in thread "main" org.hibernate.HibernateException:
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/09/25 03:30:03
Exception in thread "main" org.hibernate.HibernateException:createQuery is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
at $Proxy0.createQuery(Unknown Source)
at com.six.managerment.dao.impl.HibernateGenericDao.listAll(HibernateGenericDao.java:101)
at com.six.managerment.dao.impl.UserDaoImpl.main(UserDaoImpl.java:249)
我用下面的查询数据库
public T findById(Serializable id) {
return (T)getSession().get(persistClass,id);
}
用 getSessionFactory().getCurrentSession();获得session时就出上面的错误,如果用 getSessionFactory().openSession()获得session时 就不出错,有人说删除配置文件的 thread
或者修改 org.springframework.orm.hibernate3.SpringSessionContext就可以了,我试了不行,提示:org.hibernate.HibernateException:No Session found for current thread
获得Session
private Session getSession() {
return HibernateUtil.getSessionFactory().getCurrentSession();
}
配置文件如下:
thread
5
false
false
再麻烦你一下(只有最后这么点财富值了,不要嫌少啊)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
at $Proxy0.createQuery(Unknown Source)
at com.six.managerment.dao.impl.HibernateGenericDao.listAll(HibernateGenericDao.java:101)
at com.six.managerment.dao.impl.UserDaoImpl.main(UserDaoImpl.java:249)
我用下面的查询数据库
public T findById(Serializable id) {
return (T)getSession().get(persistClass,id);
}
用 getSessionFactory().getCurrentSession();获得session时就出上面的错误,如果用 getSessionFactory().openSession()获得session时 就不出错,有人说删除配置文件的 thread
或者修改 org.springframework.orm.hibernate3.SpringSessionContext就可以了,我试了不行,提示:org.hibernate.HibernateException:No Session found for current thread
获得Session
private Session getSession() {
return HibernateUtil.getSessionFactory().getCurrentSession();
}
配置文件如下:
thread
5
false
false
再麻烦你一下(只有最后这么点财富值了,不要嫌少啊)
修改你的getSession方法
if(session == null || !session.isopen()){
return HibernateUtil.getSessionFactory().getCurrentSession();
}
再问: private Session getSession() { return HibernateUtil.getSessionFactory().getCurrentSession(); } 怎么加(session == null || !session.isopen()),没有session这个参数啊
再答: package com.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class HibernateTool { private static SessionFactory sessionFactory; private static ThreadLocal threadLocal = new ThreadLocal(); private static Session session; static{ try { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); System.out.println("sessionFactory ini false"); } } public static Session getSession(){ session = threadLocal.get(); if (session == null || !session.isOpen()) { session = sessionFactory.getCurrentSession(); } threadLocal.set(session); return session; } }
再问: 还是不行,运行空指针异常java.lang.NullPointerException,不过还是要谢谢你了
if(session == null || !session.isopen()){
return HibernateUtil.getSessionFactory().getCurrentSession();
}
再问: private Session getSession() { return HibernateUtil.getSessionFactory().getCurrentSession(); } 怎么加(session == null || !session.isopen()),没有session这个参数啊
再答: package com.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class HibernateTool { private static SessionFactory sessionFactory; private static ThreadLocal threadLocal = new ThreadLocal(); private static Session session; static{ try { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); System.out.println("sessionFactory ini false"); } } public static Session getSession(){ session = threadLocal.get(); if (session == null || !session.isOpen()) { session = sessionFactory.getCurrentSession(); } threadLocal.set(session); return session; } }
再问: 还是不行,运行空指针异常java.lang.NullPointerException,不过还是要谢谢你了
Exception in thread "main" org.hibernate.HibernateException:
org.hibernate.HibernateException:createCriteria is not valid
org.hibernate.HibernateException: Could not find datasource
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsEx
org.hibernate.exception.SQLGrammarException:could not execut
Exception in thread "main" java.sql.SQLException:用尽的 Results
帮我找下哪错了啊说是Exception in thread "main" java.lang.Error:Unresol
Exception in thread "Thread-2" java.lang.NullPointerExceptio
org.hibernate.Session是什么
Exception in thread "AWT-EventQueue-0" java.lang.StringIndex
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexO
org.hibernate.hql.ast.QuerySyntaxException:3Q