EntityManager and transactions
Latest stable Based on Bluetape4k release 1.11.0
Use one transaction owner
Section titled “Use one transaction owner”withNewEntityManager creates an EntityManager and transaction, commits on success, rolls back on failure, and closes the manager. Do not nest it inside a Spring @Transactional operation that already owns these resources.
val id = emf.withNewEntityManager { em -> val account = Account("user@example.com") em.persist(account) account.identifier}In 1.11.0, rollback failure is warning-only. The original exception is preserved, but the rollback error is not attached as suppressed; retain the helper logger when diagnosing failures.
Continue with the merge result
Section titled “Continue with the merge result”save calls merge for a persisted entity outside the current context and persist otherwise. Merge copies state into a managed instance; it does not make the detached input managed.
val managed = entityManager.save(detached)managed.name = "new name"delete ignores an entity without an ID and merges a detached entity before removal. deleteById starts with a reference, but SQL, foreign-key, and optimistic-lock failures can still appear at flush or commit.
Flush and bulk operations
Section titled “Flush and bulk operations”Hibernate may delay SQL until flush or commit. Add an explicit flush() in tests that must pin the database failure point.
findAll defaults to Int.MAX_VALUE; production callers must pass a bound. deleteAll<T>() is a JPQL bulk delete, so it bypasses entity callbacks, cascades, and managed state. Flush first and clear the persistence context afterward.
entityManager.flush()val deleted = entityManager.deleteAll<ExpiredSession>()entityManager.clear()currentSessionImpl and currentConnection use Hibernate internals. Keep them inside narrow Hibernate-specific integrations.
Executable tests
Section titled “Executable tests”./gradlew :bluetape4k-hibernate:test --tests '*EntityManagerFactorySupportTest'./gradlew :bluetape4k-hibernate:test --tests '*EntityManagerSupportTest'