# EntityManager.refresh not working?

**URL:** https://discourse.hibernate.org/t/entitymanager-refresh-not-working/7392
**Category:** Hibernate ORM
**Created:** [March 15, 2023, 9:20pm UTC](https://discourse.hibernate.org/t/entitymanager-refresh-not-working/7392 "2023-03-15T21:20:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![robscala](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/robscala/32/2167_2.png) [@robscala](https://discourse.hibernate.org/u/robscala)
#### Post date: [March 15, 2023, 9:20pm UTC](https://discourse.hibernate.org/t/entitymanager-refresh-not-working/7392/1 "2023-03-15T21:20:24Z")

</div>

Hi,

I may be misunderstanding something. I want to update an entity with the latest fields from the database. When I call EntityManager.refresh(person), the entity doesn’t have the new values as I expect. I am not in a transaction. If I retrieve another copy with find, it does have the new values, but I want to use the original instance.

```auto
Person person = entityManager.find(Person.class, 1696L);
System.out.println("Person: " + person.getFirstName());
TimeUtilities.sleep(10000); // The name is changed in another process during this time
entityManager.refresh(person); // Should retrieve new name
System.out.println("Person: " + person.getFirstName()); // Shows old name
// Get the same record in another entity:
Person person2 = entityManager.find(Person.class, 1696L);
System.out.println("Person: " + person2.getFirstName()); // Shows new name

```

What am I missing?

---

<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: [March 16, 2023, 3:20pm UTC](https://discourse.hibernate.org/t/entitymanager-refresh-not-working/7392/2 "2023-03-16T15:20:14Z")

</div>

The code doesn’t really make sense. If you use the same `EntityManager`, the `Person#1696` will be added to the persistence context after the first `find()` call. A subsequent `find()` call will just return that existing instance without going to the database.  
Refresh on the other hand will refresh the currently managed instance, and should actually throw an exception if the passed entity is not a managed instance.

So whatever your real code is doing, the example you are showing makes no sense.

---

<div class="post-metadata">

### Author: ![robscala](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/robscala/32/2167_2.png) [@robscala](https://discourse.hibernate.org/u/robscala)
#### Post date: [March 16, 2023, 7:40pm UTC](https://discourse.hibernate.org/t/entitymanager-refresh-not-working/7392/3 "2023-03-16T19:40:29Z")

</div>

Thanks for the response. My comment about another entity showed my confusion. person2 should be the same instance as person. But it wasn’t, and I guessed that was normal.

I pasted the code back in to my IDE to verify. In my little test case, I discovered that adding a **refresh** between two **find** s causes the second **find** to return a different instance, whether or not the record changed in the database. I verified in my debugger that they were separate instances.

Definitely weird behavior. I wonder if it has anything to do with level 2 cache. I tried to turn that off.

---

<div class="post-metadata">

### Author: ![robscala](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/robscala/32/2167_2.png) [@robscala](https://discourse.hibernate.org/u/robscala)
#### Post date: [March 16, 2023, 8:24pm UTC](https://discourse.hibernate.org/t/entitymanager-refresh-not-working/7392/4 "2023-03-16T20:24:53Z")

</div>

I tried simplifying my Person entity. I removed cascade-refresh on some @ManyToOne and @OneToOne fields, and the test code worked correctly.

---

<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: [March 17, 2023, 7:44am UTC](https://discourse.hibernate.org/t/entitymanager-refresh-not-working/7392/5 "2023-03-17T07:44:50Z")

</div>

It would be best if you could try to reproduce this with a test case([hibernate-test-case-templates/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java)), and if you can, attach that to a new JIRA ticket in the issue tracker([https://hibernate.atlassian.net](https://hibernate.atlassian.net)).

---

<div class="post-metadata">

### Author: ![robscala](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/robscala/32/2167_2.png) [@robscala](https://discourse.hibernate.org/u/robscala)
#### Post date: [March 23, 2023, 8:47pm UTC](https://discourse.hibernate.org/t/entitymanager-refresh-not-working/7392/6 "2023-03-23T20:47:32Z")

</div>

I created a test case and got it to fail in hibernate 6.1 and 6.2, while it succeeded in 5.6

Then I noticed that I had a mis-configured bidirectional OneToOne relationship. After fixing that, it works.

Thanks for your help, beikov!
