首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hibernate控制台应用程序中的异常

问Hibernate控制台应用程序中的异常
EN

Stack Overflow用户
提问于 2015-05-07 16:56:03
回答 1查看 210关注 0票数 1

我有一个使用Hibernate构建的简单控制台应用程序。当我运行它时,它会抛出一个异常,而且我不知道问题是什么。

我的代码是:

代码语言:javascript
复制
 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)

EN

回答 1

Stack Overflow用户

发布于 2015-05-07 17:06:37

我怀疑您的Hibernate配置不适合当前会话。

要么您缺少hibernate.current_session_context配置,要么在hibernate配置中使用下面的属性

代码语言:javascript
复制
<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocal‌​SessionContext</property>

请使用下面的属性

代码语言:javascript
复制
<property name="hibernate.current_session_context_class">thread</property>

有关Hibernate当前会话的更多信息,请访问此链接。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30107346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档