Friday, June 1, 2012

ClassCastException Gotcha With Hibernate Search

When using Hibernate Search, you'd think the following line will limit the results to only those matching the provided entity class:
QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Cat.class).get();

Unfortunately, it doesn't. It won't just return Cats, if may even return Dogs if they are also indexed, which will in turn cause ClassCastExceptions.

To limit to only cats, you'd need to specify the entity class in this line:
FullTextQuery searchQuery = fullTextSession.createFullTextQuery(luceneQuery,Cat.class);

No comments:

Post a Comment