我正在实现一些测试样本来学习neo4j和spring数据,感谢cineast项目和其他样本,比如spring-data hello-worlds ......
不幸的是我现在面临的一个问题我不明白,即使我已经花了一些时间在我的代码上的eclipse调试器和其他示例代码尝试compage,所以找到一个解决方案 . 这个问题对我来说特别受阻,因为我无法验证Neo4j的持久性,即使它真的应该是一个微不足道的用例......
这是我正在测试的课程:
@NodeEntity
public class SimplePersonImpl {
@GraphId
private Long nodeId;
@Indexed
private String id = null;
@Indexed(indexType=IndexType.FULLTEXT, indexName = "LastName")
private String lastName = null;
@Indexed(indexType=IndexType.FULLTEXT, indexName = "FirstName")
private String firstName = null;
public SimplePersonImpl() {
}
public SimplePersonImpl(String firstName_, String lastName_) {
this.firstName=firstName_;
this.lastName=lastName_;
this.id=this.firstName+"."+this.lastName;
}
public String getID() {
return this.id;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public void setID(String id_) {
this.id=id_;
}
public void setFirstName(String firstName_) {
this.firstName=firstName_;
}
public void setLastName(String lastName_) {
this.lastName=lastName_;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SimplePersonImpl person = (SimplePersonImpl) o;
if (nodeId == null) return super.equals(o);
return nodeId.equals(person.nodeId);
}
@Override
public int hashCode() {
return nodeId != null ? nodeId.hashCode() : super.hashCode();
}
@Override
public String toString() {
return String.format("Person{first name='%s', last name='%s'}", firstName, lastName);
}
}
我正在测试这个类感谢以下jUnit测试用例:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/yes-test-context.xml"})
@Transactional
public class SimplePersonImplTest {
@Autowired Neo4jTemplate template;
@Rollback(false)
@BeforeTransaction
public void cleanUpGraph() {
Neo4jHelper.cleanDb(template);
}
@Test @Transactional
public void persistedPersonShouldBeRetrievableFromGraphDB() {
SimplePersonImpl qqun = template.save(new SimplePersonImpl());
SimplePersonImpl retrievedQqun = template.findOne(qqun.getNodeId(), SimplePersonImpl.class);
assertEquals("retrieved person matches persisted one", qqun, retrievedQqun);
}
@Test @Transactional
public void persistedPersonWithPropertiesShouldBeRetrievableFromGraphDB() {
SimplePersonImpl qqun = template.save(new SimplePersonImpl("testFN","testLN"));
SimplePersonImpl retrievedQqun = template.findOne(qqun.getNodeId(), SimplePersonImpl.class);
assertEquals("check memory first name matches persisted one", qqun.getFirstName(), retrievedQqun.getFirstName());
assertEquals("check memory last name matches persisted one", qqun.getLastName(), retrievedQqun.getLastName());
}
}
第一个测试成功运行,但第二个测试失败 . 使用eclipse调试器检查时,我可以看到:
保存方法后
-
返回一个带有正确的firtName,lastName和id值的SimplePersonImpl qqun . 但是nodeId为null,我不明白为什么 . 但是因为这个行为在spring-data hello-worlds示例中是相同的,我猜我的问题不是来自那里 .
findOne方法中的 -
,即使我的qqun对象中的nodeId为null,qqun.getNodeId()返回1.我不会't understand from where is coming this value but let'继续
-
findOne方法返回一个retrieveQqun . 所有检索到的Qqun属性都为null,这似乎是我在firstName上的第一个assertEquals失败的原因 .
在这里,我真的不明白我做错了什么(我想我做了)但很明显有很多东西背后的spring-data neo4j persistance我不明白 . 我很乐意在这些问题上得到一些答案(为什么我在保存调用后看到nodeId = null?为什么我检索一个非null对象,所有属性都为null?...) .
可能存在一些错误的最后一点是我的测试上下文配置但是我再也看不出问题出在哪里:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="yes.ds.domain.neo4j"/>
<neo4j:config graphDatabaseService="graphDatabaseService"/>
<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/>
<!-- <neo4j:config storeDirectory="target/neo4j-db"/> -->
<neo4j:repositories base-package="yes.ds.repository.neo4j"/>
<tx:annotation-driven mode="proxy"/></beans>
谢谢
1 回答
我想我在我创建的Test-Entity中遇到了类似的问题 . 在那个特殊情况下,我忘了使用
@Transactional
注释一个getter方法 .注释
@Transactional
在NodeEntities
的getter和setter上都是必需的,否则它们的行为与您描述的相同 . 添加注释为我修复了它 .必须启用事务支持原因 .