cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Retrieve all post of a user

We have 2 nodes

@Node("User)
public class User {
    @Id
    private String id;
    private String name;
}
@Node("Post")
public class Post {

    @Id
    @GeneratedValue(generatorClass = GeneratedValue.UUIDGenerator.class)
    private UUID id;
    private String text;
    private Instant createdAt;

    @Relationship(type = "AUTHOR", direction = Relationship.Direction.INCOMING)
    private User author;
}

and we are trying to retrieve all the user's posts

@Query(value = "MATCH (author:User)-[:AUTHOR]-(post:Post) RETURN post, author",
            countQuery = "MATCH (author:User)-[:AUTHOR]-(post:Post) RETURN count(post)")
    Page<PostEntity> findPageByUserId(String userId, Pageable pageable);

We retrieve all the posts, but author is null in the Post class
How to retrieve also the author?

1 REPLY 1

You would also have to return the relationships (to be sure that only the correct types are mapped):

MATCH (author:User)<-[r:AUTHOR]-(post:Post) RETURN post, collect(r), collect(author)
would be the way to go. Please use the collects for now, even that this is a 1:1 relationship because the mapping part is a little bit picky. There is already a change but not yet released.