# Entity Not indexed automatically with JPQL Update

**URL:** <https://discourse.hibernate.org/t/entity-not-indexed-automatically-with-jpql-update/2420>\
**Category:** Hibernate Search\
**Created:** [March 8, 2019, 7:02am UTC](https://discourse.hibernate.org/t/entity-not-indexed-automatically-with-jpql-update/2420 "2019-03-08T07:02:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GauravK](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@GauravK](https://discourse.hibernate.org/u/GauravK)\
**Post date:** [March 8, 2019, 7:02am UTC](https://discourse.hibernate.org/t/entity-not-indexed-automatically-with-jpql-update/2420/1 "2019-03-08T07:02:53Z")

</div>

We have entity marked for indexing as  
@Entity  
@Table(name = “A”)  
@Indexed(index=“A”)  
public class A

Now, when this entity is updated using merge() of entitymanager, it is updated automatically in the index, but when entity is updated using JPQL, index is not getting updated.  
StringBuilder query = new StringBuilder("UPDATE A a ");  
query.append("SET a.status =‘GK’ ");  
query.append(“WHERE a.id = 12273928”);  
Query dynamicQuery = getEntityManager().createQuery(query.toString());  
dynamicQuery.executeUpdate();

Do automatic indexing only works with methods for update/delete and not with JPQL update/delete?

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [March 8, 2019, 8:53am UTC](https://discourse.hibernate.org/t/entity-not-indexed-automatically-with-jpql-update/2420/2 "2019-03-08T08:53:50Z")

</div>

> [@GauravK](#):
>
> Do automatic indexing only works with methods for update/delete and not with JPQL update/delete?

That’s right, automatic indexing only works with direct operations on the session (`persist`, …). JPQL update/delete queries are translated into SQL and sent to the database as is. As a result, Hibernate ORM (and thus Hibernate Search) are not aware of _which_ entities got updated/deleted, and cannot reflect these changes in the index.

You will need to either:

- Perform your updates directly in a session by loading the entities one by one, modifying them and peristing the changes using the Session.
- Trigger reindexing manually for the impacted entities after the fact: see [Hibernate Search 5.11.12.Final: Reference Guide](https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#manual-index-changes)

Note that’s not just a limitation of Hibernate Search. If you have a second-level cache, you will need to perform evictions manually for the entities affected by your JPQL update/delete queries, because Hibernate ORM cannot do it by itself.

As for Hibernate Search, we’re planning on solving this by adding [Debezium integration](https://hibernate.atlassian.net/browse/HSEARCH-3513), which would allow us to detect all database changes, but that’s still a long-term goal.

---

<div class="post-metadata">

**Author:** ![GauravK](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@GauravK](https://discourse.hibernate.org/u/GauravK)\
**Post date:** [March 8, 2019, 10:21am UTC](https://discourse.hibernate.org/t/entity-not-indexed-automatically-with-jpql-update/2420/3 "2019-03-08T10:21:36Z")

</div>

Thanks Yrodiere for the confirmation
