我有一个使用Hibernate构建的简单控制台应用程序。当我运行它时,它会抛出一个异常,而且我不知道问题是什么。
我的代码是:
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class TestEmployee {
public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(Employee.class);
config.configure("hibernate.cfg.xml");
new SchemaExport(config).create(true, true);
SessionFactory factory = config.buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
Employee tom = new Employee();
mehdi.setEmpId(100);
mehdi.setEmpName("Tom Hani");
session.save(mehdi);
session.getTransaction().commit();
}
}例外是:
线程"main“org.hibernate.HibernateException中的异常:没有配置CurrentSessionContext!在org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:685) at com.Hibernate.chapter1.TestEmployee.main(TestEmployee.java:20)
发布于 2015-05-07 17:06:37
我怀疑您的Hibernate配置不适合当前会话。
要么您缺少hibernate.current_session_context配置,要么在hibernate配置中使用下面的属性
<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>请使用下面的属性
<property name="hibernate.current_session_context_class">thread</property>有关Hibernate当前会话的更多信息,请访问此链接。
https://stackoverflow.com/questions/30107346
复制相似问题