Skip to content

Commit

Permalink
DATAGRAPH-354
Browse files Browse the repository at this point in the history
BeanCreationException when trying to use initial-EntitySet
  • Loading branch information
jexp committed Jun 5, 2013
1 parent af8e348 commit 8b16f7f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import org.springframework.transaction.PlatformTransactionManager;
import javax.validation.Validator;

import java.util.Collection;
import java.util.Set;

import static java.util.Arrays.asList;
Expand Down Expand Up @@ -243,7 +242,7 @@ public PlatformTransactionManager neo4jTransactionManager() throws Exception {

@Bean
public IndexCreationMappingEventListener indexCreationMappingEventListener() throws Exception {
return new IndexCreationMappingEventListener(neo4jTemplate());
return new IndexCreationMappingEventListener(indexProvider());
}

@Bean
Expand All @@ -254,6 +253,7 @@ public GraphDatabase graphDatabase() {
return new DelegatingGraphDatabase(graphDatabaseService);
}

// didn't help @DependsOn({"neo4jTemplate","neo4jTransactionManager","neo4jMappingContext"})
@Bean
public ConfigurationCheck configurationCheck() throws Exception {
return new ConfigurationCheck(neo4jTemplate(),neo4jTransactionManager());
Expand All @@ -266,7 +266,7 @@ public PersistenceExceptionTranslator persistenceExceptionTranslator() {

@Bean
public IndexProvider indexProvider() throws Exception {
return new IndexProviderImpl(neo4jMappingContext(), graphDatabase());
return new IndexProviderImpl(graphDatabase());
}

public Set<? extends Class<?>> getInitialEntitySet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
Expand Down Expand Up @@ -152,7 +151,7 @@ public void afterPropertiesSet() {
this.graphDatabase.setResultConverter(resultConverter);
this.cypherQueryExecutor = new CypherQueryExecutor(graphDatabase.queryEngineFor(QueryType.Cypher, resultConverter));
if (this.indexProvider == null) {
this.indexProvider = new IndexProviderImpl(this.mappingContext, graphDatabase);
this.indexProvider = new IndexProviderImpl(graphDatabase);
}
this.mappingInfrastructure = new MappingInfrastructure(graphDatabase, graphDatabaseService, indexProvider, resultConverter, transactionManager, typeRepresentationStrategies, entityRemover, entityPersister, entityStateHandler, cypherQueryExecutor, mappingContext, relationshipTypeRepresentationStrategy, nodeTypeRepresentationStrategy, validator, conversionService);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public <T> GraphRepository<T> repositoryFor(Class<T> clazz) {

public <S extends PropertyContainer, T> Index<S> getIndex(Class<T> type) {
notNull(type, "entity type");
return getIndexProvider().getIndex(type, null);
return getIndexProvider().getIndex(getPersistentEntity(type), null);
}

public <S extends PropertyContainer> Index<S> getIndex(String name) {
Expand All @@ -130,7 +130,7 @@ public <S extends PropertyContainer> Index<S> getIndex(String name) {
}

public <S extends PropertyContainer, T> Index<S> getIndex(Class<T> type, String indexName, IndexType indexType) {
return getIndexProvider().getIndex(type, indexName, indexType);
return getIndexProvider().getIndex(getPersistentEntity(type), indexName, indexType);
}

/**
Expand Down Expand Up @@ -575,13 +575,14 @@ public <T extends PropertyContainer> Result<T> lookup(final Class<?> indexedType

@Override
public <T extends PropertyContainer> Index<T> 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 <T extends PropertyContainer> Index<T> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

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 {

<S extends PropertyContainer, T> Index<S> getIndex(Class<T> type);
<S extends PropertyContainer, T> Index<S> getIndex(Neo4jPersistentEntity<T> type);

<S extends PropertyContainer, T> Index<S> getIndex(Class<T> type, String indexName);
<S extends PropertyContainer, T> Index<S> getIndex(Neo4jPersistentEntity<T> type, String indexName);

@SuppressWarnings("unchecked")
<S extends PropertyContainer, T> Index<S> getIndex(Class<T> type, String indexName,
IndexType indexType);
<S extends PropertyContainer, T> Index<S> getIndex(Neo4jPersistentEntity<T> persistentEntity, String indexName, IndexType indexType);

@SuppressWarnings("unchecked")
<T extends PropertyContainer> Index<T> getIndex(String indexName);
Expand All @@ -36,11 +36,10 @@ <S extends PropertyContainer, T> Index<S> getIndex(Class<T> type, String indexNa

// TODO handle existing indexes
@SuppressWarnings("unchecked")
<T extends PropertyContainer> Index<T> createIndex(Class<T> type, String indexName,
<T extends PropertyContainer> Index<T> createIndex(Class<T> propertyContainerType, String indexName,
IndexType fullText);

<S extends PropertyContainer> Index<S> getIndex(Neo4jPersistentProperty property,
final Class<?> instanceType);
<S extends PropertyContainer> Index<S> getIndex(Neo4jPersistentProperty property, final Class<?> instanceType);
/**
* adjust your indexName for the "__types__" indices
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
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;

Expand All @@ -32,43 +31,42 @@
* @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 <S extends PropertyContainer, T> Index<S> getIndex(Class<T> type) {
public <S extends PropertyContainer, T> Index<S> getIndex(Neo4jPersistentEntity<T> type) {
return getIndex(type, null);
}

@Override
public <S extends PropertyContainer, T> Index<S> getIndex(Class<T> type, String indexName) {
public <S extends PropertyContainer, T> Index<S> getIndex(Neo4jPersistentEntity<T> type, String indexName) {
return getIndex(type, indexName, null);
}

@Override
@SuppressWarnings("unchecked")
public <S extends PropertyContainer, T> Index<S> getIndex(Class<T> type, String indexName, IndexType indexType) {
if (type == null) {
public <S extends PropertyContainer, T> Index<S> getIndex(Neo4jPersistentEntity<T> 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<S>) graphDatabase.getIndex(indexName);
if (isNodeEntity || isRelationshipEntity) return (Index<S>) graphDatabase.getIndex(indexName);
throw new IllegalArgumentException("Wrong index type supplied: " + type + " expected Node- or Relationship-Entity");
}

if (persistentEntity.isNodeEntity())
if (isNodeEntity)
return (Index<S>) createIndex(Node.class, indexName, indexType);
if (persistentEntity.isRelationshipEntity())
if (isRelationshipEntity)
return (Index<S>) createIndex(Relationship.class, indexName, indexType);
throw new IllegalArgumentException("Wrong index type supplied: " + type + " expected Node- or Relationship-Entity");
}
Expand All @@ -89,21 +87,21 @@ public boolean isNode(Class<? extends PropertyContainer> type) {
// TODO handle existing indexes
@Override
@SuppressWarnings("unchecked")
public <T extends PropertyContainer> Index<T> createIndex(Class<T> type, String indexName, IndexType fullText) {
return graphDatabase.createIndex(type, indexName, fullText);
public <T extends PropertyContainer> Index<T> createIndex(Class<T> propertyContainerType, String indexName, IndexType fullText) {
return graphDatabase.createIndex(propertyContainerType, indexName, fullText);
}

@Override
public <S extends PropertyContainer> Index<S> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
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;

/**
* @author mh
* @since 12.04.12
*/
public class IndexCreationMappingEventListener implements ApplicationListener<MappingContextEvent<Neo4jPersistentEntity<?>, Neo4jPersistentProperty>> {
private Neo4jTemplate template;
public IndexCreationMappingEventListener(Neo4jTemplate template) {
this.template = template;
private IndexProvider indexProvider;
public IndexCreationMappingEventListener(IndexProvider indexProvider) {
this.indexProvider = indexProvider;
}

@Override
Expand All @@ -43,12 +43,12 @@ public void onApplicationEvent(MappingContextEvent<Neo4jPersistentEntity<?>, Neo

private void ensureEntityIndexes(Neo4jPersistentEntity<?> 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<Neo4jPersistentProperty>() {
@Override
public void doWithPersistentProperty(Neo4jPersistentProperty property) {
if (property.isIndexed()) {
template.getIndex(property, entityType);
indexProvider.getIndex(property, entityType);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static class Config {
Neo4jTemplate neo4jTemplate;
@Autowired
PlatformTransactionManager transactionManager;
@Autowired
@Autowired(required = false)
Neo4jMappingContext mappingContext;
@Autowired(required = false)
PersonRepository personRepository;
Expand Down Expand Up @@ -100,7 +100,7 @@ public void injectionForExistingHighlyAvailableGraphDatabaseService() {
public void injectionForCodeConfiguredExistingGraphDatabaseService() {
assertInjected("-code");
}
@Test @Ignore("BeanCreationException DATAGRAPH-354")
@Test
public void injectionForBasePackageOfEntities() {
Config config = assertInjected("-entities");
Collection<Neo4jPersistentEntityImpl<?>> entities = config.mappingContext.getPersistentEntities();
Expand Down

0 comments on commit 8b16f7f

Please sign in to comment.