如何使用 Neo4j 从存储库中获取特定列

How To Fetch Specific Column From Repository Using Neo4j

我在 Java Spring.
上获取某些特定列时遇到问题 我正在使用 Java Spring Boot 和 GraphDB 作为我的数据库,我使用模型和存储库来访问数据库(例如:我有 userRepository,我使用 userRepository.FindAll 并且所有记录都加载在他们所有的领域)。
例如,我有一个 table 和 column/field:

@NodeEntity
public class User {
    @GraphId 
    Long id;

    @NotNull
    @Indexed(unique=true)
    private String username;

    @NotNull
    private String password;

    @NotNull
    private String firstName;

    @NotNull
    private String lastName;

    @NotNull
    private String role;

    @NotNull
    private Boolean active;

    public User() {

    }

    public User(String username, String password, String firstName,
            String lastName, String role, Boolean active) {
        super();
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.role = role;
        this.active = active;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }
}

像这样的存储库:

public interface UserRepository extends GraphRepository<User> {

}

我没有放 UserRepository.FindAll 因为它已经在 GraphRepository

上实现了

我不需要加载 "Password" 字段。我应该怎么做才能在不加载 "Password" 字段的情况下加载记录?

使用带有 @QueryResult

注释的 class
@QueryResult
public class UserSimple {
    String username;
    String role;
}

在 UserRepository 界面中:

@Query("MATCH (u:USER) RETURN u.username as username, u.role as role")
List<UserSimple> findAllSimple();

http://docs.spring.io/spring-data/data-neo4j/docs/current/reference/html/#reference_programming-model_mapresult

编辑:(评论中问题的答案)

这些用@QueryResult注解的classes很容易成为内部classes,这完全取决于你。

要return 图表ID 在return 子句中使用ID(u) as id