【问题标题】:Neo4j GraphRepository derived finder methodsNeo4j GraphRepository 派生的查找器方法
【发布时间】:2012-09-08 21:38:12
【问题描述】:

我对 GraphRepository 的派生查找器方法有疑问。

短版:

User foundUser1 = userRepository.findByEmail("test@gmail.com");
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("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 是否是某种关键字? (注意我的@GraphIdnodeId

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>

【问题讨论】:

    标签: neo4j spring-data-neo4j


    【解决方案1】:

    这可能是数字字段的特殊索引的问题。我在这里提出了一个问题:https://jira.springsource.org/browse/DATAGRAPH-294

    【讨论】:

    • 我猜派生的查找器在幕后使用 findByPropertyValue 但显然并非如此。谢谢
    • 不,他们生成密码查询,findByPropertyValue 只是一个简单的索引查找,派生的查找器是/可以更复杂
    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 2014-09-10
    • 1970-01-01
    • 2021-03-15
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多