# Manual re-indexing with Spring Repositories

**URL:** <https://discourse.hibernate.org/t/manual-re-indexing-with-spring-repositories/4777>\
**Category:** Hibernate Search\
**Created:** [November 16, 2020, 6:33pm UTC](https://discourse.hibernate.org/t/manual-re-indexing-with-spring-repositories/4777 "2020-11-16T18:33:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![buncieboy](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@buncieboy](https://discourse.hibernate.org/u/buncieboy)\
**Post date:** [November 16, 2020, 6:33pm UTC](https://discourse.hibernate.org/t/manual-re-indexing-with-spring-repositories/4777/1 "2020-11-16T18:33:41Z")

</div>

I am having an issue whenever I try to manually re-index my data. Currently all interaction is done through a JPA repository, and so when ever we save an object it is reflected in the index as intended. We want to add in manual re-indexing as we use an external ES cluster, and if that ever becomes unavailable we want to be able to re-index only the most recent objects, instead of the 5+ years of data we have.

We get the objects by calling variations of the JPA repository methods, and some custom methods (findById, findCreatedBetween etc).

To get the full text entity manager we are calling org.hibernate.search.jpa.Search.getFullTextEntityManager on an EntityManager instance. We have tried autowiring an EntityManagerFactory to create new EntityManager, and we have tried using the @PersistenceContext annotation within a repository which we then extend from our object repository. Both times when we call fullTextEntityManager.index(object) we get a TransientObjectException “The instance was not associated with this session”.

I appreciate this is a spring specific issue, but if anyone has an idea as to how we can get the correct session/entity manager it would be greatly appreciated, as I am pulling my hair out trying to solve this one!

---

<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:** [November 17, 2020, 7:29am UTC](https://discourse.hibernate.org/t/manual-re-indexing-with-spring-repositories/4777/2 "2020-11-17T07:29:00Z")

</div>

When you use Spring’s injected entity manager, it’s not the actual entity manager, it’s a proxy. What it does under the hood is it creates a new entity manager for every thread and transaction, and delegates to it.

In short, you have to take care to:

- Call `Search.getFullTextEntityManager` _after_ you started your transaction, when the actual entity manager has been created by Spring.
- Always trigger reindexing in the same transaction that loaded the object.
- Always trigger reindexing in the same thread that loaded the object.

---

<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:** [November 17, 2020, 8:20am UTC](https://discourse.hibernate.org/t/manual-re-indexing-with-spring-repositories/4777/3 "2020-11-17T08:20:29Z")

</div>

From what I can see in [the comments of this StackOverflow question](https://stackoverflow.com/questions/64862319/how-to-get-entitymanager-from-spring-jpa-repository), it seems you solved your problem and it was indeed related to mixing entities from different transactions.

---

<div class="post-metadata">

**Author:** ![buncieboy](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@buncieboy](https://discourse.hibernate.org/u/buncieboy)\
**Post date:** [November 18, 2020, 3:37pm UTC](https://discourse.hibernate.org/t/manual-re-indexing-with-spring-repositories/4777/4 "2020-11-18T15:37:32Z")

</div>

Hi @yrodiere, thanks for the help, it was indeed the incorrect usage of transactions that was causing the error, silly mistake on my part!
