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

时间:2015-02-23 09:46:10

标签: spring neo4j spring-data-neo4j

我有关于在Java Spring上获取一些特定专栏的问题 我使用Java Spring Boot和GraphDB作为我的数据库,我使用Model和Repository访问数据库(例如:我有userRepository,我使用userRepository.FindAll,所有记录都在他们的所有字段中加载)。
对于Instance,我有一个包含列/字段的表:

@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上实现了它

我不需要加载密码&#34;领域。如何加载记录而不加载&#34;密码&#34;场?

1 个答案:

答案 0 :(得分:2)

使用注释为@QueryResult

的班级
@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注释的类很容易成为内部类,这完全取决于你。

要返回图形ID,请在return子句中使用ID(u) as id