Hibernate Search query searches all tables instead of only the specified class' table -
i have abstract class product subclassed producta, productb , productc.
, classes producta, productb , productc mapped database tables products_a, products_b , products_c respectively.
now want perform full-text search on producta entities hibernate search.
wrote code expected producta entities database, found in log (as follows) executed hibernate search query searched tables products_a, products_b , products_c instead of table products_a expected.
i want producta entities, why productb , products_c tables searched? there way fix this?
log
you can see following working log outputted hibernate besides products_a table, products_b , products_c tables searched.
hibernate: select this_.id id1_2_0_, this_.name name2_2_0_, this_.feature feature3_2_0_, this_.created_date created_4_2_0_, this_.modified_date modified5_2_0_, this_.feature_a1 feature_1_3_0_, this_.feature_a2 feature_2_3_0_, this_.feature_b1 feature_1_4_0_, this_.feature_b2 feature_2_4_0_, this_.feature_c1 feature_1_5_0_, this_.feature_c2 feature_2_5_0_, this_.clazz_ clazz_0_ ( select id, name, feature, created_date, modified_date, feature_a1, feature_a2, null::varchar feature_b1, null::varchar feature_b2, null::varchar feature_c1, null::varchar feature_c2, 1 clazz_ from products_a union all select id, name, feature, created_date, modified_date, null::varchar feature_a1, null::varchar feature_a2, feature_b1, feature_b2, null::varchar feature_c1, null::varchar feature_c2, 2 clazz_ from products_b union all select id, name, feature, created_date, modified_date, null::varchar feature_a1, null::varchar feature_a2, null::varchar feature_b1, null::varchar feature_b2, feature_c1, feature_c2, 3 clazz_ from products_c ) this_ (this_.id in (?))
code
entity classes
here entity classes product, producta, productb , productc.
public abstract class product { @id protected long id; @field protected string name; @field protected string feature; protected date createddate; protected date modifieddate; // getters , setters... } @entity @indexed public class producta extends product { @field private string featurea1; @field private string featurea2; public producta() { } // getters , setters... }
the productb , productc classes similar producta class.
hibernate mapping file
product.hbm.xml
union-subclass element used reflect subclass relationship between product class , producta, productb , productc classes.
<hibernate-mapping package="com.raychen518.study.hibernate"> <class name="product" abstract="true"> <id name="id" column="id"> <generator class="increment" /> </id> <property name="name" column="name" /> <property name="feature" column="feature" /> <property name="createddate" type="timestamp" column="created_date" /> <property name="modifieddate" type="timestamp" column="modified_date" /> <union-subclass name="producta" table="products_a"> <property name="featurea1" column="feature_a1" /> <property name="featurea2" column="feature_a2" /> </union-subclass> <union-subclass name="productb" table="products_b"> <property name="featureb1" column="feature_b1" /> <property name="featureb2" column="feature_b2" /> </union-subclass> <union-subclass name="productc" table="products_c"> <property name="featurec1" column="feature_c1" /> <property name="featurec2" column="feature_c2" /> </union-subclass> </class> </hibernate-mapping>
hibernate configuration file
hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="connection.driver_class">org.postgresql.driver</property> <property name="connection.url">jdbc:postgresql://localhost:5432/test</property> <property name="connection.username">postgres</property> <property name="connection.password">admin</property> <property name="connection.pool_size">1</property> <property name="dialect">org.hibernate.dialect.postgresqldialect</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.internal.nocacheprovider</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">validate</property> <!-- setting hibernate search --> <property name="hibernate.search.lucene_version">lucene_current</property> <property name="hibernate.search.default.directory_provider">filesystem</property> <property name="hibernate.search.default.indexbase">hibernate.search.test/lucene/indexes</property> <mapping resource="product.hbm.xml" /> </session-factory> </hibernate-configuration>
application launcher class
the productmanager class contains main method, serves application launcher. starts hibernate search indexing process, clears products_a, products_b , products_c tables , inserts sample product data them, , performs full-text search using hibernate search.
confuses me have specified target entity producta.class in following statement query query = fulltextsession.createfulltextquery(lucenequery, producta.class); in method searchproducts(). why hibernate search search productb , productc entities?
public class productmanager { public static void main(string[] args) throws interruptedexception { productmanager productmanager = new productmanager(); productmanager.indexallproducts(); productmanager.deleteallproducts(); productmanager.generatesomeproducts(); productmanager.searchproducts(); } private void indexallproducts() throws interruptedexception { fulltextsession fulltextsession = search.getfulltextsession(hibernateutil.getsessionfactory().getcurrentsession()); fulltextsession.createindexer().startandwait(); } public void deleteallproducts() { session session = hibernateutil.getsessionfactory().getcurrentsession(); session.begintransaction(); @suppresswarnings("unchecked") list<product> results = session.createquery("from product").list(); (product result : results) { session.delete(result); } session.gettransaction().commit(); } public void generatesomeproducts() { session session = hibernateutil.getsessionfactory().getcurrentsession(); session.begintransaction(); session.save(new producta("feature001", "featurea1001", "featurea2001", new date())); session.save(new producta("feature002", "featurea1002", "featurea2002", new date())); session.save(new producta("feature003", "featurea1003", "featurea2003", new date())); session.save(new productb("feature001", "featureb1001", "featureb2001", new date())); session.save(new productb("feature002", "featureb1002", "featureb2002", new date())); session.save(new productb("feature003", "featureb1003", "featureb2003", new date())); session.save(new productc("feature001", "featurec1001", "featurec2001", new date())); session.save(new productc("feature002", "featurec1002", "featurec2002", new date())); session.save(new productc("feature003", "featurec1003", "featurec2003", new date())); session.gettransaction().commit(); } private void searchproducts() { fulltextsession fulltextsession = search.getfulltextsession(hibernateutil.getsessionfactory().getcurrentsession()); fulltextsession.begintransaction(); querybuilder querybuilder = fulltextsession.getsearchfactory().buildquerybuilder().forentity(producta.class).get(); org.apache.lucene.search.query lucenequery = querybuilder.keyword().onfields("feature").matching("feature002").createquery(); // set 2nd method parameter using "product.class" products of types producta, productb , productc. // set 2nd method parameter using "producta.class" products of types producta. query query = fulltextsession.createfulltextquery(lucenequery, producta.class); @suppresswarnings("unchecked") list<product> queryresults = query.list(); (product queryresult : queryresults) { system.out.println("queryresult: " + queryresult); } fulltextsession.gettransaction().commit(); } }
i fixed https://hibernate.atlassian.net/browse/hsearch-2301 following stackoverflow question.
it hasn't been released yet patch rather small , localized in 1 file: https://github.com/hibernate/hibernate-search/pull/1122/files should able apply locally on 5.5 branch.
use https://patch-diff.githubusercontent.com/raw/hibernate/hibernate-search/pull/1122.diff raw diff file.
update fixed in 5.5.4.final: http://in.relation.to/2016/06/29/polishing-polishing-and-more-polishing-hibernate-search-5-5-4-final/
Comments
Post a Comment