# EntityManager.merge creates duplicates

**URL:** <https://discourse.hibernate.org/t/entitymanager-merge-creates-duplicates/1680>\
**Category:** Hibernate ORM\
**Created:** [November 9, 2018, 9:33am UTC](https://discourse.hibernate.org/t/entitymanager-merge-creates-duplicates/1680 "2018-11-09T09:33:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sissbruecker](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sissbruecker](https://discourse.hibernate.org/u/sissbruecker)\
**Post date:** [November 9, 2018, 9:33am UTC](https://discourse.hibernate.org/t/entitymanager-merge-creates-duplicates/1680/1 "2018-11-09T09:33:00Z")

</div>

Yesterday we noticed an issue where a call to `EntityManager.merge` for an existing, unattached entity that is presumably not in the session would create a duplicate. This has never happened before or again afterwards, but at that point in time it happened 6 times in a row.

Here is the order of events plus what I deducted from the logs:

- An updated, detached entity (_Rule_ with the ID 9) is received through a request to the REST API
- **This entity existed in the database at that point in time**
- The application uses a `session-per-request` strategy, the entity has not been touched prior to the `merge` call, thus should not be in the session
- Merge is called with the detached entity
- `org.hibernate.loader.Loader` tries to load the entity
- `org.hibernate.loader.Loader` states that it finished loading the entity, but it seems it could not find it
- There are no exceptions or anything else that could indicate that the entity could not be loaded from the database
- Then a new insert is executed, creating a duplicate of the entity with the ID 16

Here is an excerpt of the log for a `merge` call, I marked the lines where the Loader is trying to retrieve the entity:

> **[Merge Duplicate Log](https://docs.google.com/spreadsheets/d/1PCF0zUflxrFliqBTLARIiyGSA260woGwWXA3YAI6MYw/edit?usp=sharing)**
>
> Tabellenblatt1
> 
> @ timestamp, message, logger\_name, thread\_name, level,@ version, level\_value
> 2018-11-08T12: 54: 48. 106+ 01: 00, begin, org. hibernate. engine. transaction. internal. TransactionImpl, https-jsse-nio-443-exec-1, DEBUG, 1, 10000
> ...

I can show more of the logs if that may be useful.

The method in the REST API:

```auto
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @AuthorizationRequired({AccessLevel.CCU_VIEW})
    public Rule saveRule(Rule rule) {
        ruleEditor.saveRule(rule);
        return rule;
    }

```

And in the `RuleService`:

```auto
    @Override
    public void saveRule(Rule rule) {
        updateAuthorizationChecks.mayEditRule(rule);
        if (rule.getId() > 0) {
            ruleRepository.merge(rule);
        } else {
            ruleRepository.persist(rule);
        }
        updateProjectEvents.ruleEdited(rule);
    }

```

`updateAuthorizationChecks.mayEditRule` does not access the entity in question.

So what I do not understand here is how the `org.hibernate.loader.Loader` could come to the conclusion that the entity did not exist. Again, this only happened a few times in a row, never before, never again. Can any of you give some insight on how this might have happened? Or how I may be able to reproduce this issue?

---

<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:** [November 10, 2018, 10:56am UTC](https://discourse.hibernate.org/t/entitymanager-merge-creates-duplicates/1680/2 "2018-11-10T10:56:40Z")

</div>

First, you need to replicate it using this [test case template](http://in.relation.to/2016/01/14/hibernate-jpa-test-case-template/).

If you can do that, you should open a Jira issue.
