2014-04-23 157 views
0

我開始使用Neo4j和Spring Data Neo4j開發一個項目。我希望我的程序使用已包含我的數據的本地數據庫(而不是每次啓動時加載數據),因爲我有大量需要加載到數據庫中的數據。我已經嘗試設置一個測試用例,用我的數據填充數據庫以實現此目標。然而,數據庫中的數據在我的測試完成後並沒有持續存在:我使用neo4j console/shell查看數據庫,發現它是空的。Neo4j數據庫沒有堅持使用Spring Data Neo4j

我已經構建了一個小例子項目,它也不起作用。任何洞察到我做錯了將不勝感激。

節點實體類:

@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> { } 

我的測試類:

@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> 

測試輸出:

運行personal.neo4j.DatabaseTest
一些實體= 2
實體一個
實體上的兩個

使用庫:
的Java 1.7
春3.2 .8.RELEASE
Neo4j 2.0.2
Spring Data Neo4j的3.0.2.RELEASE
JUnit的4.11

感謝您的幫助,

托馬斯

回答

相關問題