From 83258b6c2dcca272aeb90d9050d26d30485c55ee Mon Sep 17 00:00:00 2001 From: lgrignon Date: Tue, 18 Oct 2016 11:54:12 +0200 Subject: [PATCH 1/2] traits support --- build.gradle | 2 +- ...ernateSearchCapableSessionFactoryBean.java | 7 +++++ .../search/SearchMappingEntityConfig.groovy | 29 +++++++++++++------ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index c21d531..5f26e91 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { } } -version "2.0.1" +version "2.0.2" group "org.grails.plugins" apply plugin:"eclipse" diff --git a/src/main/groovy/grails/plugins/hibernate/search/HibernateSearchCapableSessionFactoryBean.java b/src/main/groovy/grails/plugins/hibernate/search/HibernateSearchCapableSessionFactoryBean.java index 7674895..c7111d3 100644 --- a/src/main/groovy/grails/plugins/hibernate/search/HibernateSearchCapableSessionFactoryBean.java +++ b/src/main/groovy/grails/plugins/hibernate/search/HibernateSearchCapableSessionFactoryBean.java @@ -35,6 +35,12 @@ import grails.orm.bootstrap.HibernateDatastoreSpringInitializer; import groovy.lang.Closure; +/** + * This bean inherits GORM session factory bean in order to initialize Hibernate + * Search right before sessionFactory instantiation + * + * @author lgrignon + */ public class HibernateSearchCapableSessionFactoryBean extends HibernateMappingContextSessionFactoryBean { private final static Logger log = LoggerFactory.getLogger(HibernateSearchCapableSessionFactoryBean.class); @@ -55,6 +61,7 @@ public HibernateSearchCapableSessionFactoryBean(GrailsApplication grailsApplicat ClosureEventTriggeringInterceptor eventTriggeringInterceptor, HibernateMappingContext hibernateMappingContext, DataSource dataSource) { + // this constructor is an awful duplication of GORM's sessionFactory bean initialization this.grailsApplication = grailsApplication; this.domainClasses = domainClasses; this.setEntityInterceptor(entityInterceptor); diff --git a/src/main/groovy/grails/plugins/hibernate/search/SearchMappingEntityConfig.groovy b/src/main/groovy/grails/plugins/hibernate/search/SearchMappingEntityConfig.groovy index 0942521..caad1c2 100644 --- a/src/main/groovy/grails/plugins/hibernate/search/SearchMappingEntityConfig.groovy +++ b/src/main/groovy/grails/plugins/hibernate/search/SearchMappingEntityConfig.groovy @@ -97,24 +97,35 @@ class SearchMappingEntityConfig { GrailsDomainClassProperty property = domainClass.getPersistentProperty(name); - EntityMapping targetEntityMapping = entityMapping; + Field backingField = null; - // find the field in the parent class hierarchy (starting from domain class itself) + // try to find the field in the parent class hierarchy (starting from domain class itself) Class currentDomainClass = domainClass.getClazz(); while (currentDomainClass != null) { try { - Field backingField = currentDomainClass.getDeclaredField(property.getName()); - - // field exists on domain class hierarchy - targetEntityMapping = searchMapping.entity( currentDomainClass ); - log.debug "> property " + backingField.getDeclaringClass() + ".$name found"; - currentDomainClass = null; + backingField = currentDomainClass.getDeclaredField(property.getName()); + break; } catch (NoSuchFieldException e) { + // and in groovy's traits + backingField = currentDomainClass.getDeclaredFields().find { field -> field.getName().endsWith('__' + property.getName()) }; + if (backingField != null) { + break; + } + currentDomainClass = currentDomainClass.getSuperclass(); } } + + if (backingField == null) { + log.warn "indexed property not found! name=" + name + " entity=" + domainClass + return; + } + + log.debug "> property " + backingField.getDeclaringClass() + ".$name found"; + + EntityMapping targetEntityMapping = searchMapping.entity( currentDomainClass ); - FieldMapping fieldMapping = targetEntityMapping.property( name, ElementType.FIELD ).field().name( args.name ?: name ) + FieldMapping fieldMapping = targetEntityMapping.property( backingField.getName(), ElementType.FIELD ).field().name( args.name ?: name ) registerIndexedProperty(fieldMapping, args) } } From 3cedb1c58ba8f9be64bd8d0fe9e454ba75243d8a Mon Sep 17 00:00:00 2001 From: lgrignon Date: Tue, 18 Oct 2016 12:11:45 +0200 Subject: [PATCH 2/2] doc for traits & changelog --- README.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 93a510c..17f7c80 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,8 @@ hibernate: Add a static search closure as following: +**Note**: You can use properties from super class and traits with no additional configuration (since 2.0.2) + ```groovy class MyDomainClass { @@ -611,13 +613,23 @@ MyDomainClass.search().list { } ``` +## Change log -## Bug tracker +### v2.0.2 +Support for indexing trait properties -Please report any issue on GitHub: +### v2.0.1 +Support for indexing inherited properties -https://github.com/mathpere/grails-hibernate-search-plugin/issues +### v2.0 +* Grails 3.1.x +* GORM 5 +* Hibernate 5.1.1 +* Hibernate Search 5.5.4 +### v1.x +* Grails 2.x +* Hibernate 4 ## Authors @@ -635,4 +647,4 @@ https://github.com/mathpere/grails-hibernate-search-plugin/issues ## License -Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0 \ No newline at end of file +Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0