I've found a solution and I've realized, how does Lucene/Hibernate Search works in connection with auto-updating indexes. The indexes are updated (internally) on transaction commit, but not directly, but on flush()
. Therefore, calling flush()
on EntityManager
does the trick while keeping @Transactional
intact for the class.
So the code should look something like this (I've added some exception handling as well):
@Test
public void testDiscoverArtworksByTitle()
{
FullTextEntityManager ftem = Search.getFullTextEntityManager(this.entityManager);
this.prepareArtworksForListing();
this.entityManager.flush(); // <-- ADDED FLUSH() CALL
ftem.flushToIndexes();
try
{
List<ArtworkListItem> listItems = this.artworkService.discoverArtworksByTitle("Die Hard");
Assert.assertNotEquals("There are some items in the list", 0, listItems.size());
}
finally
{
// housekeeping
ftem.purgeAll(ArtworkEntity.class);
}
}