Search result lost after Session.merge on a child entity when using Multi-Tenancy

Description

When using Hibernate ORM multi-tenancy and `Session.merge()` on a child entity of one marked with `@Indexed`, the tenant information is lost in the Lucene index, making the `@Indexed` entity not appear in search results.

This seems to happen when the entity being merged has a collection of entities with a newly added child in it. If the merged entity's child collection has not changed or if it has children removed, the tenant information in the Lucene index seems preserved.

I've created a test case here (it could probably be even simpler):

A work-around would be appreciated (or a pointer to what is wrong with the use case).
The corresponding SO post:
https://stackoverflow.com/questions/57146004/hibernate-search-not-returning-results-on-newly-created-entities

Environment

Hibernate 5.2.13.Final and 5.4.1.Final
Java 8
MacOS
H2 and PostgreSQL

Status

Assignee

Yoann Rodière

Reporter

Andrey Ustalakov

Labels

None

Suitable for new contributors

None

Feedback Requested

None

Fix versions

Affects versions

Priority

Minor
Configure