如何在Hibernate中使用getCurrentSession?

时间:2017-05-16 13:00:40

标签: java hibernate

配置休眠

Properties prop= new Properties();
prop.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
prop.setProperty("hibernate.connection.url", url);
prop.setProperty("hibernate.connection.username", user);
prop.setProperty("hibernate.connection.password", password);
concreteSessionFactory = new AnnotationConfiguration()
        .addPackage("Main.*")
        .addProperties(prop)
        .addAnnotatedClass(DeviceDataSet.class)
        .buildSessionFactory();
public SessionFactory sessionFactory() {
    return concreteSessionFactory;
}

我用

public long insertNewJournalNote(JournalDataSet journalDataSet) {
    Session session = sessionFactory.openSession();
    MySqlDAO dao = new MySqlDAO(session);
    return dao.insertNewJournalNote(journalDataSet);
 }

使用getCurrentSession,我得到:

  

线程“main”中的异常org.hibernate.HibernateException:没有配置CurrentSessionContext

1 个答案:

答案 0 :(得分:0)

也许这一行可以解决您的问题: prop.setProperty("hibernate.current_session_context_class", thread);