# starting the transaction after persist in hibernate

**URL:** <https://discourse.hibernate.org/t/starting-the-transaction-after-persist-in-hibernate/7840>\
**Category:** Hibernate ORM\
**Created:** [June 19, 2023, 7:38am UTC](https://discourse.hibernate.org/t/starting-the-transaction-after-persist-in-hibernate/7840 "2023-06-19T07:38:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sakib\_Shahriar](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sakib_shahriar/32/2499_2.png) [@Sakib\_Shahriar](https://discourse.hibernate.org/u/Sakib_Shahriar)\
**Post date:** [June 19, 2023, 7:38am UTC](https://discourse.hibernate.org/t/starting-the-transaction-after-persist-in-hibernate/7840/1 "2023-06-19T07:38:00Z")

</div>

I am new to JPA and Hibernate. I was testing its transaction management last evening and noticed something. I was able to persist the entity even though I started the transaction after calling save method and also if I don’t commit the transaction, its doesn’t persist. I am so confused about the workflow of the hibernate transaction. Here is the code snippet:

```auto
 Session session = sessionFactory.openSession();
 firstUser = createUser("aaa@aa.com", "a", "yyy", true);
 session.save(firstUser);
 session.beginTransaction().commit();

```

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 19, 2023, 8:06am UTC](https://discourse.hibernate.org/t/starting-the-transaction-after-persist-in-hibernate/7840/2 "2023-06-19T08:06:12Z")

</div>

Changes that are flushed to a database withing one session/transaction are only visible to other sessions/transactions if the transaction which did the changes also committed, that’s just how things work.

> I was able to persist the entity even though I started the transaction after calling save

Hibernate has a mode where it defers flushing to the database until the transaction commits instead of immediately. I guess your model simply allows deferring the flush and that’s why you are seeing this.

---

<div class="post-metadata">

**Author:** ![Sakib\_Shahriar](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sakib_shahriar/32/2499_2.png) [@Sakib\_Shahriar](https://discourse.hibernate.org/u/Sakib_Shahriar)\
**Post date:** [June 19, 2023, 8:34am UTC](https://discourse.hibernate.org/t/starting-the-transaction-after-persist-in-hibernate/7840/3 "2023-06-19T08:34:49Z")

</div>

then how my entity (which I saved) is aligned with the transaction? whats the role of the transaction in this case as entity is not persisting without commit.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 19, 2023, 2:01pm UTC](https://discourse.hibernate.org/t/starting-the-transaction-after-persist-in-hibernate/7840/4 "2023-06-19T14:01:37Z")

</div>

If you don’t even understand the most basic concepts like a database transaction, you maybe shouldn’t start learning this on the fly by using Hibernate. Buy a book about SQL first so you understand the basics.
