You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Data Neo4J项目中@QueryResult注解无法识别及导入失败问题求助

Fixing "cannot find symbol" for @QueryResult in Spring Data Neo4j

Hey there, let's break down why you're seeing that error and how to fix it:

The Root Cause

The @QueryResult annotation and the org.springframework.data.neo4j.annotation package were removed starting from Spring Data Neo4j (SDN) 6.x — which is the version included in recent Spring Boot starters (like Spring Boot 2.7.x and later). Your spring-boot-starter-data-neo4j dependency is pulling in this newer version, so those old annotations no longer exist.

Instead of using @QueryResult, SDN now supports cleaner ways to map custom query results:

1. Use Interface Projections

Define a simple interface with getter methods matching the fields returned by your query. SDN will automatically create a proxy implementation to map the results:

// Projection interface
public interface UserProjection {
    String getName();
    Integer getAge();
}

// In your Neo4j Repository
@Repository
public interface UserRepository extends Neo4jRepository<User, Long> {
    @Query("MATCH (u:User) WHERE u.age > $age RETURN u.name, u.age")
    List<UserProjection> findUsersOlderThan(Integer age);
}

2. Use a Record or POJO

For more control (or if you need to add logic), use a Java Record (Java 16+) or a plain POJO with matching field names (and a no-args constructor for POJOs):

Using a Record:

public record UserResult(String name, Integer age) {}

// In your Repository
@Query("MATCH (u:User) WHERE u.age > $age RETURN u.name AS name, u.age AS age")
List<UserResult> findUsersOlderThan(Integer age);

For pre-Java 16 POJOs:

public class UserResult {
    private String name;
    private Integer age;

    // No-args constructor required
    public UserResult() {}

    // Getters and setters
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public Integer getAge() { return age; }
    public void setAge(Integer age) { this.age = age; }
}

If you absolutely need to use @QueryResult, you can pin your Spring Boot starter to a version that uses SDN 5.x (Spring Boot 2.6.x or earlier). Add a version tag to your dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-neo4j</artifactId>
    <version>2.6.13</version> <!-- Last version with SDN 5.x -->
</dependency>

Note: This is not advised because older versions are no longer receiving security updates or bug fixes.


内容的提问来源于stack exchange,提问作者Iza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 13:12:32