Hi,
Having a
QueryBuilder for multiple entities is at the moment not possible. It is an outstanding feature.
It is important to understand though that this does not mean that you cannot take the Lucene query generate by the query builder and apply a completely different set of filter classes. Let me explain. The most common usecase would be:
Code:
QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().
forEntity( Foo.class ).get();
Query query = qb.keyword()
.onField( "bar" )
.matching("fubar")
.createQuery();
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, Foo.class);
fullTextQuery.list();
You are creating a query using the query builder for a certain entity and then apply the same entity as filter to the
createFullTextQuery call. However, the Lucene query generated by the query builder does have no "enforced" connection to the entity passed to
forEntity. The passed entity is in this case not a query restriction, but it just provides context for the query builder to apply automatic field conversion (you can eg pass an actual
Date instance into matching which automatically gets transformed to a query string).
The entities passed to
createFullTextQuery, however, apply filtering on the specified types. This also means that if you build a query for entity
Foo and you have a second entity Bar which also has a field
bar you could do something like this:
Code:
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, Bar.class);
I hope that was not too confusing. Arguably the query builder API implies different behavior. I guess the documentation could be cleared or maybe the query builder should actually add an entity restriction.
--Hardy