I have some model objects that are Hibernate entities, and others that cannot be (because I don't own the code). I need transactions that span updates to both objects. Basically, I want to do this:
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();
In JDBC, starting a transaction is nothing more than setting autoCommit=false on the connection (AFAICT). I don't know exactly what Hibernate does to start a transaction. Will there be any problem mixing the two kinds of updates like this?