Skip to content

Commit

Permalink
Merge pull request #25 from lgrignon/master
Browse files Browse the repository at this point in the history
Support for indexing traits property
  • Loading branch information
lgrignon authored Oct 18, 2016
2 parents 0f86427 + 3cedb1c commit 982ec7a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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

Expand All @@ -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
Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
}
}

version "2.0.1"
version "2.0.2"
group "org.grails.plugins"

apply plugin:"eclipse"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 982ec7a

Please sign in to comment.