# EntityManager.merge functionality difference between hibernate 4.1.9 and hibernate 5.2.11

**URL:** <https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746>\
**Category:** Hibernate ORM\
**Created:** [May 11, 2018, 6:10pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746 "2018-05-11T18:10:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashwani\_agarwal](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@ashwani\_agarwal](https://discourse.hibernate.org/u/ashwani_agarwal)\
**Post date:** [May 11, 2018, 6:10pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746/1 "2018-05-11T18:10:02Z")

</div>

EntityManager.merge() functionality difference between hibernate 4.1.9 and hibernate 5.2.11.

As the queries are getting fired even though there is changes in the object after .merge()

which was not in hibernate 4.1.9 version.

Can you guys please help

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [May 11, 2018, 6:36pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746/2 "2018-05-11T18:36:04Z")

</div>

What queries are you talking about. Add a code example and the SQL queries that get executed.

---

<div class="post-metadata">

**Author:** ![ashwani\_agarwal](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@ashwani\_agarwal](https://discourse.hibernate.org/u/ashwani_agarwal)\
**Post date:** [May 11, 2018, 6:49pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746/3 "2018-05-11T18:49:18Z")

</div>

insert and update queries like

insert into child\_table()  
insert into child\_table()  
insert into child\_table()  
then update query

update parent\_table()

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [May 11, 2018, 7:02pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746/4 "2018-05-11T19:02:15Z")

</div>

It’s still not clear what you are talking about. Real code and real logs are needed instead.

---

<div class="post-metadata">

**Author:** ![ashwani\_agarwal](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@ashwani\_agarwal](https://discourse.hibernate.org/u/ashwani_agarwal)\
**Post date:** [May 11, 2018, 7:19pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746/5 "2018-05-11T19:19:35Z")

</div>

So below is the example.

In v4.1.9

```auto
public void daoPersist(obj)
{
      entityManager.merge(obj);
      obj.setA(a);
      //some logic
      obj.setB(b);

      //The queries to db get executed at this point ie. at the end of the function
}

```

whereas in v5.2.11

```auto
public void daoPersist(obj)
{
      entityManager.merge(obj);

      //The queries to db get executed at this point ie. at this point of the function

      obj.setA(a);
      //some logic
      obj.setB(b);
}

```

Hence as you can see in the above code that in v5.2.11 the since the db calls are happening before setters are getting called the correct data isn’t getting persisted in the db.

We are not able to figure out why in the 2 versions of hibernate the internal calls to db are happening at 2 different places in the function.

Kindly help. 🙂

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [May 11, 2018, 7:31pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746/6 "2018-05-11T19:31:17Z")

</div>

This line of code is wrong:

> [@ashwani\_agarwal](#):
>
> entityManager.merge(obj);

It should be:

```
obj = entityManager.merge(obj);

```

For more details, check out [this article](https://vladmihalcea.com/jpa-persist-and-merge/).

More, why do you call `merge` in a method which is called `daoPersist`? Merge is for reattaching entities. It’s not a global save method. Use `persist` for new entities.

---

<div class="post-metadata">

**Author:** ![ashwani\_agarwal](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@ashwani\_agarwal](https://discourse.hibernate.org/u/ashwani_agarwal)\
**Post date:** [May 11, 2018, 7:57pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746/7 "2018-05-11T19:57:41Z")

</div>

The daoPersist function is just a name. We make sure that out daoPersist function gets called only when the obj.getId() != null (this is in an outer function though) ie. and the id is not null only when this obj exists in the db.  
We are checking our hibernate debug logs and we observed that only the update queries were fired against the merge() statement whereas it should have been actually firing the select queries and then the update queries.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [May 11, 2018, 8:13pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746/8 "2018-05-11T20:13:57Z")

</div>

Merge always issues a select if the entity is not already managed. An update is executed only if the entity gets modified. That’s how it’s designed to work.

---

<div class="post-metadata">

**Author:** ![ashwani\_agarwal](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@ashwani\_agarwal](https://discourse.hibernate.org/u/ashwani_agarwal)\
**Post date:** [May 11, 2018, 8:43pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746/9 "2018-05-11T20:43:53Z")

</div>

Our issue is that the same piece of code works for v4.1.9 but doesn’t work for v5.2.11

As explained in the functions above, in v5.2.11 the insert queries get fired to the db right when the entityManager.merge(obj) statement gets executed it doesn’t wait for setters to execute at all

whereas in v4.1.9 the select/insert queries get fired to the db right at the end ie. after the setters get executed (looks like when transaction ends) and not when the entityManager.merge(obj) statement gets executed.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [May 11, 2018, 10:44pm UTC](https://discourse.hibernate.org/t/entitymanager-merge-functionality-difference-between-hibernate-4-1-9-and-hibernate-5-2-11/746/10 "2018-05-11T22:44:41Z")

</div>

Try to replicate it with this test case template:

[http://in.relation.to/2016/01/14/hibernate-jpa-test-case-template/](http://in.relation.to/2016/01/14/hibernate-jpa-test-case-template/)
