有一张表 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)
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)
或者在hibernate.cfg.xml中加入:
<property name="current_session_context_class">thread</property>
是不是你自己构造session时的问题
Session session = sessionFactory.getCurrentSession();
用 openSession 试试
例外说个题外话 查询就别加事务了···