使用Neo4j测试spring存储库时出现NotInTransactionException

时间:2015-04-17 14:48:08

标签: neo4j spring-data spring-data-neo4j graph-databases

我在测试存储库时遇到异常:org.neo4j.graphdb.NotInTransactionException

我的域类:

@NodeEntity
public class Question {

    @GraphId Long nodeId;

    @Indexed(unique = true)
    String id;

    String title;

    public Question(String id, String title) { 
        this.id = id;
        this.title = title; 
    }
}

我的存储库

public interface QuestionRepository extends GraphRepository<Question>,
        NamedIndexRepository<Question> {

    Question findById(String id);
}

问题库存测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/users-test-context.xml"})
@Transactional
public class QuestionRepositoryTest {

    @Autowired QuestionRepository questionRepository;

    @Autowired Neo4jOperations template;

    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testCreateQuestion() throws Exception {
        Question question = template.save(new Question("1", "help me"));

        Question foundQuestion = this.questionRepository.findById("1");

        assertEquals("created and found quesion are equal", question, foundQuestion);
    }

}

此bean的配置:users-test-context.xml

<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="com.example"/>

<neo4j:config graphDatabaseService="graphDatabaseService" base-package="com.example.domain"/>
<neo4j:repositories base-package="com.example.repository"/>

<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/>
<tx:annotation-driven mode="proxy"/>

我是Neo4J的新手所以我无法解决问题所在? 请帮忙!

1 个答案:

答案 0 :(得分:0)

在接口QuestionRepository

中将该方法注释为transactional
@Transactional
Question findById(String id);