@QueryResult 不起作用

The @QueryResult doesn't work

我正在使用 Spring Data Neo4j 4 (SDN4),并尝试 return 多个实体使用 @QueryResult

没有抛出异常,但是@QueryResult对象中的一切都是null 该模型很简单,如 (:User)-[:ROLE]-(:Role)

有谁知道根本原因是什么?

用户模型

@NodeEntity
public class User implements UserDetails {

    @GraphId
    private Long id;

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

    private String userId; 

    @Relationship(type="ROLE")
    private Set<Role> roles = new HashSet<>();   

    public ADUser getaDUser() {
        return aDUser;
    }
    public void setaDUser(ADUser aDUser) {
        this.aDUser = aDUser;
    }
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }



}

榜样

@NodeEntity
public class Role implements java.io.Serializable {

    @GraphId Long id;

    private String name;

    public Role(){}
    public Role(String name){
        this.name = name;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

}

查询结果

@QueryResult
public class UserPermission {

     private User user;
     private  List<Role> roles;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}

用户存储库

public interface UserRepository extends  GraphRepository<User>{

    public User findByUserId(String userId);

    @Query(" Match (u:User)-[:ROLE]->(r) return u as user , collect(r) as roles ")
    public UserPermission findUserWithRoles();

}

日志是

        2015-08-05 17:56:18 INFO  Neo4jSession:461 - --------- new request ----------
2015-08-05 17:56:18 INFO  Neo4jSession:461 - getOrCreateTransaction() being called on thread: 1
2015-08-05 17:56:18 INFO  Neo4jSession:461 - Session identity: org.neo4j.ogm.session.delegates.TransactionsDelegate@1dcfae07
2015-08-05 17:56:18 INFO  Neo4jSession:461 - There is no existing transaction, creating a transient one
2015-08-05 17:56:18 INFO  DefaultRequest:57 - POST http://localhost:7474/db/data/transaction/commit, request: {"statements":[{"statement":"Match (u:User)-[:ROLE]->(r) return u as user , collect(r) as roles","parameters":{},"resultDataContents":["row"],"includeStats":false}]}
2015-08-05 17:56:18 INFO  DefaultRequest:86 - Response is OK, creating response handler
2015-08-05 17:56:18 WARN  SingleUseEntityMapper:97 - Unable to find property: roles on class: model.UserPermission for writing
2015-08-05 17:56:18 WARN  SingleUseEntityMapper:97 - Unable to find property: user on class: model.UserPermission for writing
2015-08-05 17:56:18 INFO  JsonResponse:103 - Closing HttpResponse

目前不支持将实体映射到 @QueryResult。 因此,您不必 returning UserRole,而是必须 return 用户 ID 和角色 ID,然后使用 load 来补充它们。

或者,如果您正在寻找一种按角色查找用户的简单方法,您可以尝试以下方法 List<User> findByRolesName(String roleName)UserRepository