diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/config/Neo4jConfiguration.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/config/Neo4jConfiguration.java index 34cc4f8f2..ff9a28727 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/config/Neo4jConfiguration.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/config/Neo4jConfiguration.java @@ -60,7 +60,6 @@ import org.springframework.data.support.IsNewStrategyFactory; import org.springframework.transaction.PlatformTransactionManager; import javax.validation.Validator; -import java.util.Collection; import java.util.Set; import static java.util.Arrays.asList; @@ -243,7 +242,7 @@ public abstract class Neo4jConfiguration { @Bean public IndexCreationMappingEventListener indexCreationMappingEventListener() throws Exception { - return new IndexCreationMappingEventListener(neo4jTemplate()); + return new IndexCreationMappingEventListener(indexProvider()); } @Bean @@ -254,6 +253,7 @@ public abstract class Neo4jConfiguration { return new DelegatingGraphDatabase(graphDatabaseService); } + // didn't help @DependsOn({"neo4jTemplate","neo4jTransactionManager","neo4jMappingContext"}) @Bean public ConfigurationCheck configurationCheck() throws Exception { return new ConfigurationCheck(neo4jTemplate(),neo4jTransactionManager()); @@ -266,7 +266,7 @@ public abstract class Neo4jConfiguration { @Bean public IndexProvider indexProvider() throws Exception { - return new IndexProviderImpl(neo4jMappingContext(), graphDatabase()); + return new IndexProviderImpl(graphDatabase()); } public Set> getInitialEntitySet() { diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/MappingInfrastructureFactoryBean.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/MappingInfrastructureFactoryBean.java index 3f9a2ad73..edea767d9 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/MappingInfrastructureFactoryBean.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/MappingInfrastructureFactoryBean.java @@ -21,7 +21,6 @@ import org.neo4j.graphdb.Relationship; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.convert.ConversionService; -import org.springframework.data.auditing.IsNewAwareAuditingHandler; import org.springframework.data.mapping.context.MappingContextIsNewStrategyFactory; import org.springframework.data.neo4j.annotation.QueryType; import org.springframework.data.neo4j.conversion.ResultConverter; @@ -152,7 +151,7 @@ public class MappingInfrastructureFactoryBean implements FactoryBean Index getIndex(Class type) { notNull(type, "entity type"); - return getIndexProvider().getIndex(type, null); + return getIndexProvider().getIndex(getPersistentEntity(type), null); } public Index getIndex(String name) { @@ -130,7 +130,7 @@ public class Neo4jTemplate implements Neo4jOperations, ApplicationContextAware { } public Index getIndex(Class type, String indexName, IndexType indexType) { - return getIndexProvider().getIndex(type, indexName, indexType); + return getIndexProvider().getIndex(getPersistentEntity(type), indexName, indexType); } /** @@ -575,13 +575,14 @@ public class Neo4jTemplate implements Neo4jOperations, ApplicationContextAware { @Override public Index getIndex(String indexName, Class indexedType) { - return getIndexProvider().getIndex(indexedType, indexName); + final Neo4jPersistentEntityImpl persistentEntity = indexedType==null ? null : getPersistentEntity(indexedType); + return getIndexProvider().getIndex(persistentEntity, indexName); } @Override public Index getIndex(Class indexedType, String propertyName) { final Neo4jPersistentProperty property = getPersistentProperty(indexedType, propertyName); - if (property == null) return getIndexProvider().getIndex(indexedType, null); + if (property == null) return getIndexProvider().getIndex(getPersistentEntity(indexedType), null); return getIndexProvider().getIndex(property, indexedType); } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProvider.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProvider.java index 6b4930a05..1cfa20b8e 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProvider.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProvider.java @@ -17,17 +17,17 @@ package org.springframework.data.neo4j.support.index; import org.neo4j.graphdb.PropertyContainer; import org.neo4j.graphdb.index.Index; +import org.springframework.data.neo4j.mapping.Neo4jPersistentEntity; import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty; public interface IndexProvider { - Index getIndex(Class type); + Index getIndex(Neo4jPersistentEntity type); - Index getIndex(Class type, String indexName); + Index getIndex(Neo4jPersistentEntity type, String indexName); @SuppressWarnings("unchecked") - Index getIndex(Class type, String indexName, - IndexType indexType); + Index getIndex(Neo4jPersistentEntity persistentEntity, String indexName, IndexType indexType); @SuppressWarnings("unchecked") Index getIndex(String indexName); @@ -36,11 +36,10 @@ public interface IndexProvider { // TODO handle existing indexes @SuppressWarnings("unchecked") - Index createIndex(Class type, String indexName, + Index createIndex(Class propertyContainerType, String indexName, IndexType fullText); - Index getIndex(Neo4jPersistentProperty property, - final Class instanceType); + Index getIndex(Neo4jPersistentProperty property, final Class instanceType); /** * adjust your indexName for the "__types__" indices * diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProviderImpl.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProviderImpl.java index 9d566a121..b3b3c85b3 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProviderImpl.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/index/IndexProviderImpl.java @@ -21,9 +21,8 @@ import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.index.Index; import org.springframework.data.neo4j.annotation.Indexed; import org.springframework.data.neo4j.core.GraphDatabase; +import org.springframework.data.neo4j.mapping.Neo4jPersistentEntity; import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty; -import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext; -import org.springframework.data.neo4j.support.mapping.Neo4jPersistentEntityImpl; import static org.springframework.data.neo4j.support.ParameterCheck.notNull; @@ -32,43 +31,42 @@ import static org.springframework.data.neo4j.support.ParameterCheck.notNull; * @since 17.10.11 */ public class IndexProviderImpl implements IndexProvider { - private final Neo4jMappingContext mappingContext; private final GraphDatabase graphDatabase; - public IndexProviderImpl(Neo4jMappingContext mappingContext, GraphDatabase graphDatabase) { - this.mappingContext = mappingContext; + public IndexProviderImpl(GraphDatabase graphDatabase) { this.graphDatabase = graphDatabase; } @Override - public Index getIndex(Class type) { + public Index getIndex(Neo4jPersistentEntity type) { return getIndex(type, null); } @Override - public Index getIndex(Class type, String indexName) { + public Index getIndex(Neo4jPersistentEntity type, String indexName) { return getIndex(type, indexName, null); } + @Override @SuppressWarnings("unchecked") - public Index getIndex(Class type, String indexName, IndexType indexType) { - if (type == null) { + public Index getIndex(Neo4jPersistentEntity persistentEntity, String indexName, IndexType indexType) { + if (persistentEntity == null) { notNull(indexName, "indexName"); return getIndex(indexName); } - - final Neo4jPersistentEntityImpl persistentEntity = mappingContext.getPersistentEntity(type); + final Class type = persistentEntity.getType(); if (indexName == null) indexName = customizeIndexName(Indexed.Name.get(type), type); final boolean useExistingIndex = indexType == null; - + final boolean isNodeEntity = persistentEntity.isNodeEntity(); + final boolean isRelationshipEntity = persistentEntity.isRelationshipEntity(); if (useExistingIndex) { - if (persistentEntity.isNodeEntity() || persistentEntity.isRelationshipEntity()) return (Index) graphDatabase.getIndex(indexName); + if (isNodeEntity || isRelationshipEntity) return (Index) graphDatabase.getIndex(indexName); throw new IllegalArgumentException("Wrong index type supplied: " + type + " expected Node- or Relationship-Entity"); } - if (persistentEntity.isNodeEntity()) + if (isNodeEntity) return (Index) createIndex(Node.class, indexName, indexType); - if (persistentEntity.isRelationshipEntity()) + if (isRelationshipEntity) return (Index) createIndex(Relationship.class, indexName, indexType); throw new IllegalArgumentException("Wrong index type supplied: " + type + " expected Node- or Relationship-Entity"); } @@ -89,21 +87,21 @@ public class IndexProviderImpl implements IndexProvider { // TODO handle existing indexes @Override @SuppressWarnings("unchecked") - public Index createIndex(Class type, String indexName, IndexType fullText) { - return graphDatabase.createIndex(type, indexName, fullText); + public Index createIndex(Class propertyContainerType, String indexName, IndexType fullText) { + return graphDatabase.createIndex(propertyContainerType, indexName, fullText); } @Override public Index getIndex(Neo4jPersistentProperty property, final Class instanceType) { final Indexed indexedAnnotation = property.getAnnotation(Indexed.class); - final Class declaringType = property.getOwner().getType(); + final Neo4jPersistentEntity declaringType = property.getOwner(); final String providedIndexName = providedIndexName(indexedAnnotation); final Indexed.Level level = indexingLevel(indexedAnnotation); - String indexName = customizeIndexName(Indexed.Name.get(level, declaringType, providedIndexName, instanceType), instanceType); + String indexName = customizeIndexName(Indexed.Name.get(level, declaringType.getType(), providedIndexName, instanceType), instanceType); if (!property.isIndexed() || property.getIndexInfo().getIndexType() == IndexType.SIMPLE) { return getIndex(declaringType, indexName, IndexType.SIMPLE); } - String defaultIndexName = customizeIndexName(Indexed.Name.get(level, declaringType, null, instanceType.getClass()), instanceType); + String defaultIndexName = customizeIndexName(Indexed.Name.get(level, declaringType.getType(), null, instanceType.getClass()), instanceType); if (providedIndexName==null || providedIndexName.equals(defaultIndexName)) { throw new IllegalStateException("Index name for "+property+" must differ from the default name: "+defaultIndexName); } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/IndexCreationMappingEventListener.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/IndexCreationMappingEventListener.java index 8e8ce7558..c6a994cd3 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/IndexCreationMappingEventListener.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/IndexCreationMappingEventListener.java @@ -21,7 +21,7 @@ import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.context.MappingContextEvent; import org.springframework.data.neo4j.mapping.Neo4jPersistentEntity; import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty; -import org.springframework.data.neo4j.support.Neo4jTemplate; +import org.springframework.data.neo4j.support.index.IndexProvider; import org.springframework.data.neo4j.support.index.IndexType; /** @@ -29,9 +29,9 @@ import org.springframework.data.neo4j.support.index.IndexType; * @since 12.04.12 */ public class IndexCreationMappingEventListener implements ApplicationListener, Neo4jPersistentProperty>> { - private Neo4jTemplate template; - public IndexCreationMappingEventListener(Neo4jTemplate template) { - this.template = template; + private IndexProvider indexProvider; + public IndexCreationMappingEventListener(IndexProvider indexProvider) { + this.indexProvider = indexProvider; } @Override @@ -43,12 +43,12 @@ public class IndexCreationMappingEventListener implements ApplicationListener entity) { final Class entityType = entity.getType(); - @SuppressWarnings("unchecked") Index index = template.getIndex(entityType, null, IndexType.SIMPLE); + indexProvider.getIndex(entity, null, IndexType.SIMPLE); entity.doWithProperties(new PropertyHandler() { @Override public void doWithPersistentProperty(Neo4jPersistentProperty property) { if (property.isIndexed()) { - template.getIndex(property, entityType); + indexProvider.getIndex(property, entityType); } } }); diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/config/DataGraphNamespaceHandlerTests.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/config/DataGraphNamespaceHandlerTests.java index 1f57b3dce..ae367fd80 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/config/DataGraphNamespaceHandlerTests.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/config/DataGraphNamespaceHandlerTests.java @@ -56,7 +56,7 @@ public class DataGraphNamespaceHandlerTests { Neo4jTemplate neo4jTemplate; @Autowired PlatformTransactionManager transactionManager; - @Autowired + @Autowired(required = false) Neo4jMappingContext mappingContext; @Autowired(required = false) PersonRepository personRepository; @@ -100,7 +100,7 @@ public class DataGraphNamespaceHandlerTests { public void injectionForCodeConfiguredExistingGraphDatabaseService() { assertInjected("-code"); } - @Test @Ignore("BeanCreationException DATAGRAPH-354") + @Test public void injectionForBasePackageOfEntities() { Config config = assertInjected("-entities"); Collection> entities = config.mappingContext.getPersistentEntities();