Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // some DB operations tx.commit(); boolean isCommitted = tx.wasCommitted(); System.out.println("Transaction committed? " + isCommitted); } catch (Exception ex) { if (tx != null) tx.rollback(); } finally { session.close(); }
CriteriaBuilder builder = session.getCriteriaBuilder(); CriteriaQueryIn this example, we use the `wasCommitted()` method to check whether the transaction was committed after executing a select query from the `Employee` table. We first start a transaction, execute the query, and then check if it was committed. Finally, we commit the transaction. The `wasCommitted()` method is part of the `org.hibernate` package library.query = builder.createQuery(String.class); Root root = query.from(Employee.class); query.select(root.get("name")).where(builder.equal(root.get("department"), "IT")); Transaction tx = session.beginTransaction(); List employees = session.createQuery(query).getResultList(); boolean isCommitted = tx.wasCommitted(); System.out.println("Transaction committed? " + isCommitted); tx.commit();