JPA Model testing with Hibernate Statistics


Often we write JPA Entity classes an have to ensure in a larger team that refactoring doesn’t create performance problems or logical errors in the entity model usual problems are:

  • Missing fetch joins in Queries
    • Which leads to slow fetch joins
  • Wrong direction of Entity relations
    • which leads to updates after inserts
    • or even to deletes and reinserts of data
  • Wrong transaction boundaries
    • Which leads to multiple transactions for one logical business operation
public class HibernateAsserts {
  private final Statistics statistics;

  public HibernateAsserts(EntityManager entityManager) {
    try (Session session = entityManager.unwrap(org.hibernate.Session.class)) {
      SessionFactory factory = session.getSessionFactory();
      statistics = factory.getStatistics();
  public HibernateAsserts assertTrxCount(int expected) {
    long value = statistics.getTransactionCount();
    if (value != expected) {
      fail("Expected " + expected + " TransactionCount, but found " + value);
    return this;
  public HibernateAsserts assertInsertCount(int expected) {
    long value = statistics.getEntityInsertCount();
    if (value != expected) {
      fail("Expected " + expected + " EntityInsertCount, but found " + value);
    return this;

  public void reset() {

  public void logSummary() {

In your test

class MyTestClassTest {

  @Autowired private SubjectUnderTest subject;
  @Autowired private HibernateAsserts hibernateAsserts;
  void textMyBusinessMethod() {
    // GIVEN
    // custom data setup for the test

    // WHEN
    hibernateAsserts.reset(); // ensure the setup is not in the stats
    // THEN
    // any business asserts

Paul Sterl has written 51 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>