简体   繁体   中英

How to use getCurrentSession in Hibernate?

Configuration 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;
}

I use

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

With getCurrentSession , I get:

Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM