I'm starting a project using Neo4j and Spring Data Neo4j. I want my program to use a local database that already contains my data (as opposed to loading the data each time on startup) since I have a lot of data that needs to be loaded into the database. I've tried setting up a test case that populates a database with my data in order to accomplish this goal. However, the data in the database does not appear to persist after my tests have finished running: I look at the database using the neo4j console/shell and find it is empty.
I've constructed a small example project that's also not working. Any insight into what I'm doing incorrectly would be appreciated.
Node entity class:
@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;
}
}
Repository class:
public interface EntityRepository extends GraphRepository<Entity> { }
My test class:
@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>
Test output:
Running personal.neo4j.DatabaseTest
Number of entities = 2
Entity one
Entity two
Using libraries:
Java 1.7
Spring 3.2.8.RELEASE
Neo4j 2.0.2
Spring Data Neo4j 3.0.2.RELEASE
JUnit 4.11
Thanks for your help,
Thomas