java - 获取 org.hibernate.HibernateException:未配置 CurrentSessionContext
问题描述
我已经使用 hibernate 5.4 创建了一个 maven 项目,并且现在当我尝试通过getSessionFactory().getCurrentSession()
我得到异常org.hibernate.HibernateException:没有 CurrentSessionContext 配置来获取 Hibernate Session 时成功创建了 DAO!虽然我已经
<property name="hibernate.current_session_context_class">thread</property>
在hibernate.cfg.xml文件中添加了。已经检查了几个论坛,包括这个问题org.hibernate.HibernateException: No CurrentSessionContext configured but无法解决它,
这是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() 来获取会话,然后会话工厂会将会话绑定到当前上下文,因为您使用的是当前会话上下文类线程而不是 JTA。详情在这里
推荐阅读
- javascript - 按下提交按钮时,Django下拉选择不起作用
- latex - 在 Rmd pdf 输出中包装 add_header_above
- arrays - Dataweave 删除空数组
- sql-server - 批量插入在 MS SQL Server 中不起作用
- php - 将数据保存在关联表中(belonsToMany) Cakephp 3
- lambda - 将 lambda 参数完美转发给成员函数,其中成员函数是非类型模板形参
- c# - .NET Core Identity 使用 userManager 并加载其他 ApplicationUser 属性
- r - R中数据帧特定行的规范化
- java-time - Mapstruct:无法从 java.time.LocalDate 转换为 java.time.OffsetDateTime
- unity3d - 自定义生成函数 - 生成旋转