有一张表 User  字段有 id , name ,group_id代码如下:package org.qxj.hibernate;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;public class getUserTest {
public static void main(String[] args) {
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
User u = (User)session.get(User.class, 1);
System.out.println(u.getGroup().getId());
session.getTransaction().commit();
session.close();
sessionFactory.close() ;
}
}
错误:
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:620)
at org.qxj.hibernate.updateUserTest.main(updateUserTest.java:10)

解决方案 »

  1.   

    你的hibernate配置了吗?调用配置文件了吗?
      

  2.   

    如果是在web容器中运行hibernate,则在hibernate.cfg.xml中加入这句话:<property name="hibernate.current_session_context_class">jta</property>如果是在一个单独的需要进行JDBC连接的java application中运行hibernate,则这样设置:<property name="hibernate.current_session_context_class">thread</property>从别人博客上转的,看下可以不
      

  3.   

    针对使用properties文件配置hiernate的写法,使用hibernate.cfg.xml应使用第一种调用方式
    或者在hibernate.cfg.xml中加入:
     
    <property name="current_session_context_class">thread</property>
      

  4.   

    汗 你用main方法测试
    是不是你自己构造session时的问题
    Session session = sessionFactory.getCurrentSession();
    用 openSession 试试
    例外说个题外话 查询就别加事务了···