java - 获取 org.hibernate.HibernateException : No CurrentSessionContext configured

标签 java hibernate

我已经使用 hibernate 5.4 创建了一个 Maven 项目,并且现在当我尝试通过 getSessionFactory().getCurrentSession() 获取 Hibernate Session 时成功创建了 DAO。我得到 Exception org.hibernate.HibernateException: No CurrentSessionContext 配置! 尽管我已经添加了 <property name="hibernate.current_session_context_class">thread</property>hibernate.cfg.xml 文件中。已经检查了几个论坛,包括这个问题org.hibernate.HibernateException: No CurrentSessionContext configured但无法解决,

这是 hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">onetozero</property>
        <property name="hibernate.connection.url">jdbc:mysql://192.168.0.112:3306/ecdis</property>
        <property name="hibernate.connection.username">root1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <property name="show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <mapping class="Domain.Route"/>
        <mapping class="Domain.RoutePoint"/>
    </session-factory>
</hibernate-configuration>

HibernateSession 类

public class HibernateSession {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

}

这就是我通过 DAO 获取列表的方式

public List<T> list() {
        Session session = HibernateSession.getSessionFactory().getCurrentSession();
        CriteriaQuery<T> query = session.getCriteriaBuilder().createQuery(entityClass);
        query.select(query.from(entityClass));
        return session.createQuery(query).getResultList();
    }

我是 Java 和 Hibernate 的新手,也没有使用任何框架 atm,因此也可以了解一些细节。 TIA

最佳答案

使用 HibernateSession.getSessionFactory().openSession() 而不是 getCurrentSession() 来获取 session ,然后 session 工厂会将 session 绑定(bind)到当前上下文,因为您使用的是当前 session 上下文类线程而不是 JTA。 Details here

关于java - 获取 org.hibernate.HibernateException : No CurrentSessionContext configured,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59630156/

相关文章:

java - 自动填充集

java - 在 Java 中生成 3 字节(0x800 到 0xffff)UTF-8 编码

java - 是检查错误好还是异常(exception)好

java - 分别使用 Spring ApplicationContext 和 DispatcherServlet 时,网页无法打开/损坏

java 将对象转换为 PrintWriter

java - Hibernate 搜索投影 - StaticAliasToBeanResultTransformer

java - Spring mysql : Data truncation (data too long for column)

java - hibernate @EmbeddedId

java - 如何添加带有按钮的新 JFrame?

java - 如何在hibernate中获取XML格式的查询结果?