简单的关系没有与queryForObject映射(Simple relationships not mapped with queryForObject)

我有一个问题。 我正在使用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);

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

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

问候,

亚历克斯

I have one question. I am using neo4j-ogm snapshot 1.5 . I have the following classes:

@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; // }

When I am trying to do the following:

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);

The user Object (u) never contains any properties (PropertyGroup entity is not mapped).

What am I doing wrong? Any help would be appreciated.

Regards,

Alex

最满意答案

如果你使用queryForObject只返回一列 - 对象,就你的情况而言。 Neo4j OGM 1.x不支持将自定义查询结果映射到域实体,因此您将不得不返回实体ID,然后再执行一个指定自定义深度的附加load-by-id。

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

更新: OGM 2.0.0-M01的依赖项是

<dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-api</artifactId> <version>2.0.0-M01</version> </dependency> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-core</artifactId> <version>2.0.0-M01</version> </dependency>

请务必阅读自从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/

If you're using queryForObject return just one column- the object, in your case u. Neo4j OGM 1.x does not support mapping of custom query results to domain entities, so you will have to return the entity ID, and then do an additional load-by-id specifying a custom depth.

OGM 2.0 (currently 2.0.0-M01) does support mapping custom query results to entities. Your query will remain the same (i.e. return u,h,pg) but instead you'll use the query() method that returns a Result. From the result, you'll be able to get your User entity by column-name u and it'll be hydrated with the PropertyGroups it is related to.

Update: The dependencies for OGM 2.0.0-M01 are

<dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-api</artifactId> <version>2.0.0-M01</version> </dependency> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-core</artifactId> <version>2.0.0-M01</version> </dependency>

Be sure to read the about the configuration changes since you're upgrading from OGM 1.x http://neo4j.com/docs/ogm/java/2.0.0-M01/#reference_setup

A summary of new features: http://neo4j.com/blog/neo4j-ogm-2-0-milestone-1/

更多推荐