问题描述
我对GraphRepository的派发查找器方法有疑问.
I have a problem with derived finder methods of GraphRepository.
简短版本:
User foundUser1 = userRepository.findByEmail("[email protected]");
User foundUser2 = userRepository.findByPropertyValue("id", 1);
有效,但无效:
User foundUser3 = userRepository.findById(1);
长版:
test-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("[email protected]");
userRepository.save(user);
User foundUser1 = userRepository.findByEmail("[email protected]");
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
)
third assert fails since foundUser3 is null. I don't understand why this is happening while it can find with findByPropertyValue("id" ..)
. I wonder if id is some sort of keyword? (Note that my @GraphId
is called 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>
推荐答案
这可能是对数字字段进行特殊索引的问题.我在这里提出了一个问题: https://jira.springsource.org/browse/DATAGRAPH-294
This is probably an issue with special indexing of numeric fields. I raised an issue here: https://jira.springsource.org/browse/DATAGRAPH-294
这篇关于Neo4j GraphRepository派生的查找器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!