spring data neo4j 4.2.0.RELEASE自定义查询问题

时间:2017-03-28 17:09:37

标签: java spring maven cypher spring-data-neo4j

以下是代码:

@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long> {

    Person findOne(Long id);
    Person findByFirstName(String firstName);
    Person save(Person person);
    boolean exists(Long id);

    List<Person> findAll();

    @Query("MATCH (p:Person) RETURN p")
    Person get(String n);}

    @NodeEntity(label = "Person")
}

这是模型类:

@NodeEntity
public class Person{
    public Person(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Person(Long id, String firstName, String lastName){
        this(firstName, lastName);
        this.id = id;
    }

    @GraphId
    @Property(name = "id")
    @Index(unique = true)
    @Getter
    private Long id;

    @Getter
    @Property(name = "firstName")
    private String firstName;

    @Getter
    @Property(name = "lastName")
    private String lastName;
}

Spring配置:

@Configuration
@EnableNeo4jRepositories("my_package.repository")
@EnableTransactionManagement
@ComponentScan("my_package")
public class ApplicationConfiguration {

     @Bean
     public SessionFactory sessionFactory() {
         // with domain entity base package(s)
         return new SessionFactory("my_package.model");
     }

     @Bean
     public Neo4jTransactionManager transactionManager() {
         return new Neo4jTransactionManager(sessionFactory());
     }

 }

Maven pom.xml:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <lombok.version>1.16.14</lombok.version>
    <spring-data-neo4j.version>4.2.0.RELEASE</spring-data-neo4j.version>
    <lombok.version>1.16.14</lombok.version>
    <spring.version>4.3.7.RELEASE</spring.version>
    <neo4j.version>3.1.2</neo4j.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>${spring-data-neo4j.version}</version>
    </dependency>

    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-ogm-test</artifactId>
        <version>2.1.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
    </dependency>


</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

spring数据提供的默认CRUD方法工作正常,但具有自定义Cypher查询的方法不起作用。我对Cypher并不熟悉,但考虑到我到目前为止看到的其他例子它应该有效。我错过了一些依赖关系或什么? 以下是代码提出问题:

@Test
public void testFindFriendsById(){
    Person main = new Person(10L, "mainname", "mainlastnmame");

    personRepository.save(main);

    // here is that custom method from PersonRepository
    // having this query : @Query("MATCH (p:Person) RETURN p")
    Person person = personRepository.get(main.getFirstName());

    assertThat(person, is(notNullValue()));
}

有什么建议吗?

UPD:我正在使用TestServer进行测试:

protected static TestServer testServer;

@BeforeClass
public static void setupTestServer() {
    testServer = new TestServer.Builder().port(2222).build();
}

2 个答案:

答案 0 :(得分:2)

因此,事实证明Spring并没有自动将参数绑定到查询。

@Query("MATCH (p:Person) where p.firstName = {name} return p")
Person get(@Param("name") String name);

这很好用

答案 1 :(得分:0)

方法应该做什么?通过firstName获取用户?您需要添加过滤器位置:按firstName中的属性Person进行过滤。

@Query("MATCH (p:Person {firstName={0}}) RETURN p")