我对 GraphRepository 的派生查找器方法有疑问。
简洁版本:
User foundUser1 = userRepository.findByEmail("test@gmail.com");
User foundUser2 = userRepository.findByPropertyValue("id", 1);
有效,但不是:
User foundUser3 = userRepository.findById(1);
长版:
测试上下文.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);
}
用户.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 为空。我不明白为什么会发生这种情况,而它可以找到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>