Hi all,
I found a workaround when using the programmatic API, a field or a method defined in a parent Class cannot be mapped. You have to map the parent class as well in your search mapping.
Example:
Code:
class ProductCatalog {
...
protected List<ItemImpl> items;
...
public List<Item> getItems()
...
}
Code:
class ProductCatalogImpl extends ProductCatalog {
...
long catalogId;
String title;
...
}
The working mapping is:
Code:
SearchMapping mapping = new SearchMapping();
mappping
.entity(ProductCatalogImpl.class).indexed().indexName("catalog")
.property("catalogId", ElementType.FIELD).documentId().name("id")
.property("title", ElementType.METHOD).field()
.index(Index.TOKENIZED).store(Store.NO)
.property("description", ElementType.METHOD).field().name("desc")
.index(Index.TOKENIZED).store(Store.NO)
.entity(ProductCatalog.class).indexed()
.property("items", ElementType.FIELD).indexEmbedded()
.entity(ItemImpl.class).indexed().indexName("item")
.property("itemId", ElementType.FIELD).documentId().name("id")
.property("name", ElementType.METHOD).field().name("itemName")
instead of
Code:
SearchMapping mapping = new SearchMapping();
mappping
.entity(ProductCatalogImpl.class).indexed().indexName("catalog")
.property("catalogId", ElementType.FIELD).documentId().name("id")
.property("title", ElementType.METHOD).field()
.index(Index.TOKENIZED).store(Store.NO)
.property("description", ElementType.METHOD).field().name("desc")
.index(Index.TOKENIZED).store(Store.NO)
.property("items", ElementType.FIELD).indexEmbedded()
.entity(ItemImpl.class).indexed().indexName("item")
.property("itemId", ElementType.FIELD).documentId().name("id")
.property("name", ElementType.METHOD).field().name("itemName")
Looking hibernate search source code, I found in org.hibernate.search.impl.MappingModelMetadataProvider.MappingModelAnnotationReader when using the constructor: public MappingModelAnnotationReader(SearchMapping mapping, MetadataProvider delegate, AnnotatedElement el), if the annotated element is a field or a method, the declaring class is used to set the entityType instead of the mapped class (in my case : with the mapping which not work, for the field item, the entityType is ProductCatalog instead of ProductCatalogImpl).
Is it a good practice to index the class hierarchy? What about performance between a class and a class hierarchy? Thanks in advance
Regards,
Matti