【问题标题】:Null return using GraphRepository from Spring Data for Neo4j使用来自 Spring Data for Neo4j 的 GraphRepository 返回 Null
【发布时间】:2012-10-19 02:08:48
【问题描述】:

最近我一直在使用 Neo4j (version 2.1.0.BUILD-SNAPSHOT) 的 Spring Data,但遇到了与其 GraphRepository 接口相关的问题。

您可以编写一个方法,使用方法名称中包含的属性搜索 T 类的节点,如下所示:

public interface UserRepository extends GraphRepository<User> {
    public User findByUsername(String username);
    public User findById(Long id);
}

我的用户类:

@NodeEntity
public class User {

    @GraphId
    Long id;

    @Indexed
    private String username;

    private String password;

    ...
}

还有我的 applicationContext.xml:

<?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:spring-configured />
    <context:annotation-config />

    <context:component-scan base-package="package.for.services" />  
    <neo4j:repositories base-package="package.for.repositories" />

    <neo4j:config graphDatabaseService="graphDatabaseService" />
    <bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
        destroy-method="shutdown" scope="singleton">
        <constructor-arg value="/path/to/neo4j.db" />
        <constructor-arg>
            <map>
                <entry key="allow_store_upgrade" value="true" />
            </map>
        </constructor-arg>
    </bean>
</beans>

我的问题是 findById(Long id) 方法确实返回具有该特定 ID 的节点,但 findByUsername(String username) 确实返回 null 而不是具有该特定用户名的节点。

任何帮助将不胜感激。

【问题讨论】:

    标签: spring neo4j null spring-data spring-data-neo4j


    【解决方案1】:

    通过重新创建数据库解决。删除其目录并再次启动应用程序。

    我猜索引从一开始就没有正确创建,并且一旦创建,Spring Data 就无法更改它们。

    谢谢。

    【讨论】:

      猜你喜欢
      • 2014-09-10
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多