Type-safe FullTextQuery
Description
duplicates
Activity
Yoann RodièreMay 10, 2017 at 7:30 AM
Thanks for your suggestion. Unfortunately, I'm afraid this cannot be done in the current state of the APIs, or at least not without quite a bit of hacking.
The thing is, the type returned by a FullTextQuery can change after you called createFullTextQuery, and therefore previous references to the exact same query will probably break your code (they'll appear to return MyEntityType, but will return Object[] instead). The reason the returned type can change is that we have a setProjection
method on FullTextQuery
which, when called, will change the return type from whatever it was originally (your entity type) to an Object[]
.
In any case, these changes are already being discussed in HSEARCH-2225, and we are planning to do something about it in our API changes planned for Hibernate Search 6 : HSEARCH-2498. It basically involves removing the setProjection method and forcing users to specify whether they want a projection or not before they create the query.
I will close this ticket as duplicate, but feel free to re-open it if you feel that https://hibernate.atlassian.net/browse/HSEARCH-2225#icft=HSEARCH-2225 is not the same as what you proposed.
I suggest to add the methods
/** * Create a fulltext query on top of a native Lucene query returning the matching objects * of type <code>resultType</code> and their respective subclasses. * * @param resultType enforce a result type analogue to JPA queries * @param luceneQuery The native Lucene query to be run against the Lucene index. * @param typeFilters List of classes for type filtering. The query result will only return entities of * the specified types and their respective subtype. If no class is specified no type filtering will take place. * * @return A <code>FullTextTypedQuery</code> wrapping around the native Lucene query. * * @throws IllegalArgumentException if resultType is <code>null</code> or not a class or superclass annotated with <code>@Indexed</code>. */ <X> FullTextTypedQuery<X> createFullTextTypedQuery(org.apache.lucene.search.Query luceneQuery, Class<X> resultType, Class<? extends X>... typeFilters); /** * Creates a fulltext query from the given query descriptor. * * @param descriptor The query descriptor * @param resultType enforce a result type analogue to JPA queries * @param typeFilters List of classes for type filtering. The query result will only return entities of * the specified types and their respective subtype. If no class is specified no type filtering will take place. * * @return A <code>FullTextTypedQuery</code> using the given query descriptor. * * @throws IllegalArgumentException if resultType is <code>null</code> or not a class or superclass annotated with <code>@Indexed</code>. */ <X> FullTextTypedQuery<X> createFullTextTypedQuery(QueryDescriptor descriptor, Class<X> resultType, Class<? extends X>... typeFilters);
to `FullTextEntityManager` and creating a `FullTextTypedQuery` extending `TypedQuery` instead of `Query` analogue to `FullTextQuery`.
From a superficial point of view it seems to be possible, but it'd be the first step into hibernate-search for me, so I'll leave it at the suggestion. Since `FullTextEntityManager.createFullTextQuery` has a `@throws IllegalArgumentException if entityType is <code>null</code> or not a class or superclass annotated with <code>@Indexed</code>.` Javadoc which has no correspondance to the method, I'm uncertain whether this is part of a previous type-safe implementation which I didn't find.