Skip to content

Commit 982ec7a

Browse files
authored
Merge pull request mathpere#25 from lgrignon/master
Support for indexing traits property
2 parents 0f86427 + 3cedb1c commit 982ec7a

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ hibernate:
6161
6262
Add a static search closure as following:
6363
64+
**Note**: You can use properties from super class and traits with no additional configuration (since 2.0.2)
65+
6466
```groovy
6567
class MyDomainClass {
6668

@@ -611,13 +613,23 @@ MyDomainClass.search().list {
611613
}
612614
```
613615

616+
## Change log
614617

615-
## Bug tracker
618+
### v2.0.2
619+
Support for indexing trait properties
616620

617-
Please report any issue on GitHub:
621+
### v2.0.1
622+
Support for indexing inherited properties
618623

619-
https://github.com/mathpere/grails-hibernate-search-plugin/issues
624+
### v2.0
625+
* Grails 3.1.x
626+
* GORM 5
627+
* Hibernate 5.1.1
628+
* Hibernate Search 5.5.4
620629

630+
### v1.x
631+
* Grails 2.x
632+
* Hibernate 4
621633

622634
## Authors
623635

@@ -635,4 +647,4 @@ https://github.com/mathpere/grails-hibernate-search-plugin/issues
635647

636648
## License
637649

638-
Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
650+
Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ buildscript {
1010
}
1111
}
1212

13-
version "2.0.1"
13+
version "2.0.2"
1414
group "org.grails.plugins"
1515

1616
apply plugin:"eclipse"

src/main/groovy/grails/plugins/hibernate/search/HibernateSearchCapableSessionFactoryBean.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
import grails.orm.bootstrap.HibernateDatastoreSpringInitializer;
3636
import groovy.lang.Closure;
3737

38+
/**
39+
* This bean inherits GORM session factory bean in order to initialize Hibernate
40+
* Search right before sessionFactory instantiation
41+
*
42+
* @author lgrignon
43+
*/
3844
public class HibernateSearchCapableSessionFactoryBean extends HibernateMappingContextSessionFactoryBean {
3945

4046
private final static Logger log = LoggerFactory.getLogger(HibernateSearchCapableSessionFactoryBean.class);
@@ -55,6 +61,7 @@ public HibernateSearchCapableSessionFactoryBean(GrailsApplication grailsApplicat
5561
ClosureEventTriggeringInterceptor eventTriggeringInterceptor,
5662
HibernateMappingContext hibernateMappingContext, DataSource dataSource) {
5763

64+
// this constructor is an awful duplication of GORM's sessionFactory bean initialization
5865
this.grailsApplication = grailsApplication;
5966
this.domainClasses = domainClasses;
6067
this.setEntityInterceptor(entityInterceptor);

src/main/groovy/grails/plugins/hibernate/search/SearchMappingEntityConfig.groovy

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,35 @@ class SearchMappingEntityConfig {
9797

9898
GrailsDomainClassProperty property = domainClass.getPersistentProperty(name);
9999

100-
EntityMapping targetEntityMapping = entityMapping;
100+
Field backingField = null;
101101

102-
// find the field in the parent class hierarchy (starting from domain class itself)
102+
// try to find the field in the parent class hierarchy (starting from domain class itself)
103103
Class currentDomainClass = domainClass.getClazz();
104104
while (currentDomainClass != null) {
105105
try {
106-
Field backingField = currentDomainClass.getDeclaredField(property.getName());
107-
108-
// field exists on domain class hierarchy
109-
targetEntityMapping = searchMapping.entity( currentDomainClass );
110-
log.debug "> property " + backingField.getDeclaringClass() + ".$name found";
111-
currentDomainClass = null;
106+
backingField = currentDomainClass.getDeclaredField(property.getName());
107+
break;
112108
} catch (NoSuchFieldException e) {
109+
// and in groovy's traits
110+
backingField = currentDomainClass.getDeclaredFields().find { field -> field.getName().endsWith('__' + property.getName()) };
111+
if (backingField != null) {
112+
break;
113+
}
114+
113115
currentDomainClass = currentDomainClass.getSuperclass();
114116
}
115117
}
118+
119+
if (backingField == null) {
120+
log.warn "indexed property not found! name=" + name + " entity=" + domainClass
121+
return;
122+
}
123+
124+
log.debug "> property " + backingField.getDeclaringClass() + ".$name found";
125+
126+
EntityMapping targetEntityMapping = searchMapping.entity( currentDomainClass );
116127

117-
FieldMapping fieldMapping = targetEntityMapping.property( name, ElementType.FIELD ).field().name( args.name ?: name )
128+
FieldMapping fieldMapping = targetEntityMapping.property( backingField.getName(), ElementType.FIELD ).field().name( args.name ?: name )
118129
registerIndexedProperty(fieldMapping, args)
119130
}
120131
}

0 commit comments

Comments
 (0)