未使用queryForObject映射的简单关系

时间:2016-01-29 11:34:29

标签: neo4j neo4j-ogm

我有一个问题。我正在使用neo4j-ogm快照1.5。 我有以下课程:

@NodeEntity
public abstract class Entity {
    @GraphId
    protected Long id;

    @Expose
    protected String code = null;

    @Override
    public boolean equals(Object o) {
        if (this == o) 
            return true;

        if (o == null || id == null || getClass() != o.getClass()) 
            return false;

        Entity entity = (Entity) o;

        if (!id.equals(entity.id)) 
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return (id == null) ? -1 : id.hashCode();
    }

    public Long getId(){
        return id;
    }

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

    public String getCode(){
        return code;
    }

    public void setCode(String code){
        this.code = code;
    }
}



public class PropertyGroup extends Entity{

    @Expose
    private String name;

    public PropertyGroup(){

    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


public class User  extends Entity {
    private Long registration_date;

    private Long last_login_date;

    private Boolean is_admin = false;

    private String push_dev;

    private String push_id;

    private Boolean push_enabled = false;

    @Expose
    private String avatar;
    @Expose
    private String avatarUrl;
    @Expose
    private String name;
    @Expose
    private volatile String password;
    @Expose
    private int likes = 0;
    @Expose
    private int questionCount = 0;
    @Expose
    private int followersCount = 0;
    @Expose
    private boolean isFollowing = false;


//  public Set<UserPropertyRelation> properties;

//  @Relationship(type = ModelRelType.ANSWERED)
//  public Set<UserAnsweredRelation> userAnswers;
//
//  @Relationship(type = ModelRelType.LIKES)
//  private Set<LikesQuestionRelation> questionsLiked;



    @Expose
    @Relationship(type = ModelRelType.HAS_PROPERTY)
    private Set<PropertyGroup> properties;

//  private Profile userProfile;

//  private List<Fact> facts;
//  @Expose
//  @Relationship(type = ModelRelType.OWNS)
//  private List<Question> questions;

    public User(){
//      this.properties = new LinkedHashSet<UserPropertyRelation>();
//      this.userAnswers = new LinkedHashSet<UserAnsweredRelation>();
//      this.userProperties = new HashSet<PropertyGroup>();
//      this.setFacts(new ArrayList<Fact>());
        this.properties = new HashSet<PropertyGroup>();
    }

    public User(long regDate, long lastLoginDate, boolean isAdmin, 
            String pushDev, String pushId, boolean pushEnabled){
        this();
        this.registration_date = regDate;
        this.last_login_date = lastLoginDate;
        this.is_admin = isAdmin;
        this.push_dev = pushDev;
        this.push_id = pushId;
        this.push_enabled = pushEnabled;    
    }

//  public void addUserAnsweredRelation(UserAnsweredRelation answer){
//      answer.setStartNode(this);
//      this.userAnswers.add(answer);
//  }
//  
//  public Set<UserAnsweredRelation> getUserAnsweredRelations() {
//      return this.userAnswers;
//  }

//  public void setUserAnsweredRelations(Set<UserAnsweredRelation> userAnswers){
//      for(UserAnsweredRelation a : userAnswers){
//          a.setStartNode(this);
//      }
//      
//      this.userAnswers = userAnswers;
//  }
//  
//  public void addUserPropertyRelation(UserPropertyRelation rel){
//      rel.setUser(this);
//      properties.add(rel);
//  }
//  
//  public void setUserPropertyRelations(Set<UserPropertyRelation> properties){
//      for(UserPropertyRelation r: properties){
//          r.setUser(this);
//      }
//      
//      this.properties = properties;
//  }

//  public Set<UserPropertyRelation> getUserPropertyRelations(){
//      return this.properties;
//  }

    public long getRegistrationDate() {
        return registration_date;
    }

    public void setRegistrationDate(long registrationDate) {
        this.registration_date = registrationDate;
    }

    public long getLastLoginDate() {
        return last_login_date;
    }

    public void setLastLoginDate(long lastLoginDate) {
        this.last_login_date = lastLoginDate;
    }

    public boolean isAdmin() {
        return is_admin;
    }

    public void setAdmin(boolean isAdmin) {
        this.is_admin = isAdmin;
    }

    public String getPushDev() {
        return push_dev;
    }

    public void setPushDev(String pushDev) {
        this.push_dev = pushDev;
    }

    public String getPushId() {
        return push_id;
    }

    public void setPushId(String pushId) {
        this.push_id = pushId;
    }

    public boolean isPushEnabled() {
        return push_enabled;
    }

    public void setPushEnabled(boolean pushEnabled) {
        this.push_enabled = pushEnabled;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<PropertyGroup> getProperties() {
        return properties;
    }

    public void setProperties(Set<PropertyGroup> properties) {
        this.properties = properties;
    }

//  public Profile getUserProfile() {
//      return userProfile;
//  }
//
//  public void setUserProfile(Profile userProfile) {
//      this.userProfile = userProfile;
//  }

//  public Set<LikesQuestionRelation> getQuestionsLiked() {
//      return questionsLiked;
//  }
//
//  public void setQuestionsLiked(Set<LikesQuestionRelation> likes) {
//      this.questionsLiked = likes;
//  }

    public String getPassword() {
        return password;
    }

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

//  public List<Fact> getFacts() {
//      return facts;
//  }
//
//  public void setFacts(List<Fact> facts) {
//      this.facts = facts;
//  }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public int getLikes() {
        return likes;
    }

    public void setLikes(int likes) {
        this.likes = likes;
    }

    public int getQuestionCount() {
        return questionCount;
    }

    public void setQuestionCount(int questionCount) {
        this.questionCount = questionCount;
    }

    public int getFollowersCount() {
        return followersCount;
    }

    public void setFollowersCount(int followersCount) {
        this.followersCount = followersCount;
    }

    public boolean isFollowing() {
        return isFollowing;
    }

    public void setFollowing(boolean isFollowing) {
        this.isFollowing = isFollowing;
    }

//  public List<Question> getQuestions() {
//      return questions;
//  }
//
//  public void setQuestions(List<Question> questions) {
//      this.questions = questions;
//  }

当我尝试执行以下操作时:

SessionFactory sessionFactory = new SessionFactory(modelsPackageName);
        Session session = sessionFactory.openSession(url);

        String cypher = "MATCH (u:User {code: {CODE}})-[h:HAS_PROPERTY]->(pg:PropertyGroup) " +
                "RETURN u, h, pg";

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("CODE", "fc48b19ba6f8427a03d6e5990bcef99a28f55592b80fe38731cf805ed188cabf");
//      System.out.println(Util.mergeParamsWithCypher(cypher, params));
        User u = session.queryForObject(User.class, cypher, params);

用户Object(u)从不包含任何属性(未映射PropertyGroup实体)。

我做错了什么? 任何帮助将不胜感激。

此致

亚历

1 个答案:

答案 0 :(得分:0)

如果你正在使用queryForObject只返回一列 - 对象,在你的情况下u。 Neo4j OGM 1.x不支持将自定义查询结果映射到域实体,因此您必须返回实体ID,然后执行指定自定义深度的额外load-id。

OGM 2.0(目前为2.0.0-M01)支持将自定义查询结果映射到实体。您的查询将保持不变(即return u,h,pg),但您将使用返回query()的{​​{1}}方法。从结果中,您将能够通过列名Result获取您的用户实体,并且它将与它所关联的PropertyGroup一起使用。

<强>更新 OGM 2.0.0-M01的依赖关系是

u

请务必阅读有关自OGM升级后的配置更改1.x http://neo4j.com/docs/ogm/java/2.0.0-M01/#reference_setup

新功能摘要:http://neo4j.com/blog/neo4j-ogm-2-0-milestone-1/