hibernate - 混合 Hibernate 和原始 JDBC 事务
问题描述
我有一些模型对象是 Hibernate 实体,而另一些则不能(因为我不拥有代码)。我需要跨越两个对象更新的事务。基本上,我想这样做:
Transaction txn = sessionFactory.getCurrentSession().beginTransaction();
fooDao.update(...); // Hibernate-based DAO
// (Apparently the way to get the JDBC Connection.)
Connection con = sessionFactory.
getSessionFactoryOptions().getServiceRegistry().
getService(ConnectionProvider.class).getConnection();
try (var ps = con.prepareStatement(...)) {
ps.setString(...);
ps.executeUpdate();
}
txn.commit();
在 JDBC 中,启动事务无非是在连接上设置 autoCommit=false (AFAICT)。我不知道 Hibernate 究竟做了什么来启动事务。像这样混合两种更新会有什么问题吗?
解决方案
推荐阅读
- angular - 模块 '"*/node_modules/ngx-echarts/ngx-echarts"' 没有导出成员 'Ngx Echarts Service'
- typescript - 打字稿界面集
- opengl - DirectDraw Surface - 数据布局
- apache-spark - 有没有办法只打印 Spark SQL 输出?它在执行时打印有关环境的所有其他信息
- opengl - 标准化点云(用于在 OpenGL 中显示)的最有效方法是什么?
- jquery - 如何确定物品是否被丢弃
- laravel - Laravel - 如何在没有额外请求的情况下将缓存值传递给多个视图?
- ios - UISlider 作为音频搜索栏在更改时跳转到最大值
- php - 如何防止 whereRaw 自动对我的收藏进行排序?
- ios - Swift 修改通知配置