We're using Hibernate Search 4.3 along with Hibernate 4.0.1 in a JBoss 7.0.1 environment and so far it all seems to work perfectly.
However, we're now facing some indexing problems which seem to originate from our search mapping.
I'll try and break down the problem into some abstract bits.
Consider the following entity structure:
We have 3 entities, 2 of which are indexed and one is part of the others' index.
Code:
class SharedEntity
{
String key;
}
class IndexedEntityA
{
SharedEntity s;
}
class IndexedEntityB
{
SharedEntity s;
}
Our SearchFactory is configured as follows:
Code:
SearchMapping mapping = new SearchMapping();
EntityMapping entityAMapping = mapping.entity( IndexedEntityA.class );
entityAMapping .indexed();
EntityMapping sharedMappingA = entityAMapping .property( "s", ElementType.FIELD ).indexEmbedded().targetElement( SharedEntity.class ).entity( SharedEntity.class );
sharedMappingA.property( "key", ElementType.FIELD ).field().store( Store.YES );
EntityMapping entityBMapping = mapping.entity( IndexedEntityB.class );
entityBMapping .indexed();
EntityMapping sharedMappingB = entityBMapping .property( "s", ElementType.FIELD ).indexEmbedded().targetElement( SharedEntity.class ).entity( SharedEntity.class );
sharedMappingB.property( "key", ElementType.FIELD ).field().store( Store.YES );
As you can see, SharedEntity is mapped twice, since it should be part of the indices for both IndexedEntityA and IndexedEntityB. But since there's no unique back reference there's no containedIn().
What puzzles me a bit is
.entity( SharedEntity.class ); but without that, the entity mapping didn't seem to be found and thus was not mapped. Maybe that's a source of the problem, but I'm not sure about this.
So with this setup, whenever a SharedEntity is updated, Hibernate Search tries to update its index and doesn't find neither an index mapping (which is correct, since there's no explicit index for SharedEntity) nor a containedIn definition (which also seems to be correct, since there's no unique containedIn() ).
Thus, Hibernate Search throws the following exception:
Quote:
org.hibernate.search.SearchException: Unable to perform work. Entity Class is not @Indexed nor hosts @ContainedIn: class my.package.SharedEntity
I debugged into Hibernate Search and found the following spot within Hibernate Search's TransactionalWorker class:
Code:
public void performWork(Work<?> work, TransactionContext transactionContext) {
final Class<?> entityType = instanceInitializer.getClassFromWork( work );
EntityIndexBinder indexBindingForEntity = factory.getIndexBindingForEntity( entityType );
if ( indexBindingForEntity == null
&& factory.getDocumentBuilderContainedEntity( entityType ) == null ) {
throw new SearchException( "Unable to perform work. Entity Class is not @Indexed nor hosts @ContainedIn: " + entityType );
}
work = interceptWork( indexBindingForEntity, work );
...
The work is then passed to the PostTransactionWorkQueueSynchronization, but IMHO this shouldn't happen here (since SharedEntity instances should not be indexed in a standalone way).
indexBindingForEntity is null, which seems ok, because there is no index.
factory.getDocumentBuilderContainedEntity( entityType ) also returns null, which seems ok as well, since there is no contained entity definition (at least no unique one as seems to be the requirement).
I assume there's an error in the mapping, but I didn't find a solution yet.
Any ideas on this?