我对GraphRepository的派生finder方法有问题。
简短版:
User foundUser1 = userRepository.findByEmail("test@gmail.com");
User foundUser2 = userRepository.findByPropertyValue("id", 1);
有效,但不是:
User foundUser3 = userRepository.findById(1);
长版:
测试context.xml中
<context:annotation-config/>
<neo4j:config storeDirectory="data/graph.db" />
<neo4j:repositories base-package="com.blbl.repository"/>
<tx:annotation-driven mode="proxy"/>
UserGraphRepository.java:
public interface UserGraphRepository extends GraphRepository<User> {
public User findById(int id);
public User findByEmail(String email);
}
User.java:
@NodeEntity
public class User {
@GraphId private Long nodeId;
@Indexed(unique = true) private int id;
@Indexed private String email;
public User(int id) {
this.id = id;
}
// getter & setters
}
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/test-context.xml"})
@Transactional
public class UserServiceTest {
@Autowired
private UserGraphRepository userRepository;
@Autowired
private Neo4jTemplate template;
@BeforeTransaction
@Rollback(value = false)
public void cleanDb() {
Neo4jHelper.cleanDb(template);
}
@Test
public void testSaveUser() {
User user = userRepository.save(new User(1));
user.setEmail("test@gmail.com");
userRepository.save(user);
User foundUser1 = userRepository.findByEmail("test@gmail.com");
User foundUser2 = userRepository.findByPropertyValue("id", 1);
User foundUser3 = userRepository.findById(1);
assertThat(foundUser1, is(notNullValue())); // SUCCESS
assertThat(foundUser2, is(notNullValue())); // SUCCESS
assertThat(foundUser3, is(notNullValue())); // FAILS
}
}
第三个断言失败,因为foundUser3为null。我不明白为什么会发生这种情况,而findByPropertyValue("id" ..)
可以找到。我想知道 id 是否是某种关键字? (请注意,我的@GraphId
称为nodeId
)
PS:
<neo4j-version>1.8.M07</neo4j-version>
<org.springframework.version>3.1.2.RELEASE</org.springframework.version>
<org.springframework-data-neo4j.version>2.1.0.RC3</org.springframework-data-neo4j.version>