Je commence un projet utilisant Neo4j et Spring Data Neo4j. Je veux que mon programme utilise une base de données locale qui contient déjà mes données (par opposition à charger les données chaque fois au démarrage) car j'ai beaucoup de données qui doivent être chargées dans la base de données. J'ai essayé de configurer un scénario de test qui remplit une base de données avec mes données afin d'atteindre cet objectif. Cependant, les données de la base de données ne semblent pas persister après l'exécution de mes tests: Je regarde la base de données en utilisant la console/shell neo4j et je trouve qu'elle est vide.La base de données Neo4j ne persiste pas avec Spring Data Neo4j
J'ai construit un petit exemple de projet qui ne fonctionne pas non plus. Toute idée de ce que je fais de façon incorrecte serait appréciée.
Noeud classe d'entité:
@NodeEntity
public class Entity {
@GraphId private Long graphId;
private String name;
public Entity() { }
public Entity(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
classe Repository:
public interface EntityRepository extends GraphRepository<Entity> { }
Ma classe de test:
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class DatabaseTest {
@Autowired Neo4jTemplate template;
@Autowired EntityRepository entityRepository;
@Test
public void testCreatingEntities() {
Entity entity1 = new Entity("one");
Entity entity2 = new Entity("two");
template.save(entity1);
template.save(entity2);
Iterator<Entity> entityIterator = entityRepository.findAll().iterator();
List<Entity> entityList = IteratorUtils.toList(entityIterator);
System.out.println("Number of entities = " + entityList.size());
for(Entity entity : entityList) {
System.out.println("Entity " + entity.getName());
}
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:spring-configured/>
<context:annotation-config/>
<context:component-scan base-package="personal.neo4j">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<neo4j:config storeDirectory="data/test.db"
base-package="personal.neo4j"/>
<neo4j:repositories base-package="personal.neo4j"/>
<tx:annotation-driven/>
</beans>
Sortie test:
Courir personal.neo4j.DatabaseTest
Nombre d'entités = 2
entité une entité
deux
bibliothèques Utilisation:
Java 1.7
Spring 3.2 .8.RELEASE
Neo4j 2.0.2
Données de printemps Neo4j 3.0.2.RELEASE
JUnit 4.11
Merci pour votre aide,
Thomas
Génial! Exactement ce dont j'avais besoin. Je vous remercie. –