spring-boot - SpringBoot 2.3.x -> 2.4.1:不能再解开 Session 类
问题描述
升级到 Spring Boot 2.4.1 后。我现在得到一个例外
com.sun.proxy.$Proxy134 and org.hibernate.engine.spi.SessionFactoryImplementor are in unnamed module of loader 'app'
at at.myClass.core.StatusControllerBase.statusInfo(StatusControllerBase.java:89)
匹配的代码是
public class StatusControllerBase {
@Autowired
private EntityManager em;
...
public void doSomethingWithHibernate() {
final SessionFactoryImplementor entityManagerFactory = (SessionFactoryImplementor) em
.unwrap(org.hibernate.Session.class).getEntityManagerFactory();
SpringBootApplication 与 StatusControllerBase ( ) 位于不同的项目中myStatusProject
。任何的想法
- 为什么我现在得到异常?
- 如何修复异常?我想我需要在其中配置一些东西,
pom
但不知道是什么。
更新
正如 Roar S. 正确地计算出失败的演员。我需要它的主要目的是检索 JDBC 连接字符串的完整 URL。或者更准确地说,我终于对 DB 主机感兴趣了。当前工作的代码如下所示:
final Session session = em.unwrap(org.hibernate.Session.class);
final SessionFactory sessionFactory = session.getSessionFactory();
final SessionFactoryImplementor entityManagerFactory = (SessionFactoryImplementor) em
.unwrap(org.hibernate.Session.class).getEntityManagerFactory();
final DatasourceConnectionProviderImpl connectionProvider = (DatasourceConnectionProviderImpl) entityManagerFactory
.getServiceRegistry().getService(ConnectionProvider.class);
final String strUrl = ((HikariDataSource) connectionProvider.getDataSource()).getJdbcUrl().substring(5);
final URI uri = URI.create(strUrl);
ret.put("host", uri.getHost());
解决方案
在与 OP 讨论后,现有代码的目的是从连接字符串中检索主机名。如果 application.properties 中存在连接字符串,更简单的方法是执行以下操作(替换spring.datasource.url
为 application.properties/application.yml 中连接字符串的实际路径):
public class StatusControllerBase {
@Autowired
private EntityManager em;
@Value("${spring.datasource.url}")
private String datasourceUrl;
...
推荐阅读
- discord.py - 向机器人邀请者发送消息
- c# - EWS GetUserPhoto 委托给 App-Only 身份验证
- types - 为什么在agda中没有公理K的情况下,宇宙级限制在归纳族和参数化归纳类型之间表现不同
- python - 如何从python中的单值列表输出中删除方括号?
- javascript - 为什么我输入内容时文本字段和复选框会下降?
- c# - 是否可以使用 NPOI 将宏写入 excel
- c++ - 如何在c中四舍五入?
- amazon-cloudformation - 无法执行 lambda 函数。确保您已授予 CloudWatch Logs 执行函数的权限
- java - 我需要keyboard.close(); 使代码工作?
- sequelize.js - 如何不计算 sequelize 中包含的关联