Neo4j数据库未使用Spring数据Neo4j持久化
我正在启动一个使用Neo4j和Spring数据Neo4j的项目。我希望我的程序使用一个已经包含我的数据的本地数据库(而不是每次启动时都加载数据),因为我有很多数据需要加载到数据库中。为了实现这一目标,我尝试设置一个用我的数据填充数据库的测试用例。但是,在我的测试完成运行后,数据库中的数据似乎不会持久存在:我使用neo4j控制台/shell查看数据库,发现它是空的 我构建了一个小的示例项目,但它也不起作用。如果您能了解我的错误做法,我们将不胜感激 节点实体类:Neo4j数据库未使用Spring数据Neo4j持久化,neo4j,spring-data-neo4j,Neo4j,Spring Data Neo4j,我正在启动一个使用Neo4j和Spring数据Neo4j的项目。我希望我的程序使用一个已经包含我的数据的本地数据库(而不是每次启动时都加载数据),因为我有很多数据需要加载到数据库中。为了实现这一目标,我尝试设置一个用我的数据填充数据库的测试用例。但是,在我的测试完成运行后,数据库中的数据似乎不会持久存在:我使用neo4j控制台/shell查看数据库,发现它是空的 我构建了一个小的示例项目,但它也不起作用。如果您能了解我的错误做法,我们将不胜感激 节点实体类: @NodeEntity public
@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;
}
}
存储库类:
public interface EntityRepository extends GraphRepository<Entity> { }
public interface EntityRepository扩展了graphrespository{}
我的测试班:
@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());
}
}
}
@ContextConfiguration(locations=“classpath:applicationContext.xml”)
@RunWith(SpringJUnit4ClassRunner.class)
@交易的
公共类数据库测试{
@自动连线Neo4jTemplate模板;
@自连线全还原全还原;
@试验
公共无效测试创建实体(){
实体实体1=新实体(“一个”);
实体实体2=新实体(“两个”);
模板保存(entity1);
模板保存(entity2);
迭代器entityIterator=entityRepository.findAll().Iterator();
List entityList=IteratorUtils.toList(entityIterator);
System.out.println(“实体数=“+entityList.size());
for(实体: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>
测试输出:
运行personal.neo4j.DatabaseTest实体数量=2
实体一
实体二
使用库:
Java 1.7
弹簧3.2.8.释放
Neo4j 2.0.2
弹簧数据Neo4j 3.0.2.RELEASE
JUnit4.11
谢谢你的帮助 Thomas查看此线程是否有帮助: 看起来SpringJUnit4ClassRunner将回滚所有事务,除非明确告知其他事务 查看此线程是否有帮助: 看起来SpringJUnit4ClassRunner将回滚所有事务,除非明确告知其他事务 查看此线程是否有帮助: 看起来SpringJUnit4ClassRunner将回滚所有事务,除非明确告知其他事务 查看此线程是否有帮助:
看起来SpringJUnit4ClassRunner将回滚所有事务,除非明确告知其他事务 太棒了!正是我需要的。谢谢,太棒了!正是我需要的。谢谢,太棒了!正是我需要的。谢谢,太棒了!正是我需要的。非常感谢。