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.

Spring data neo4j - relationship object remains null when using @Query

Hi All

I've started building some Java project based on the neo4j graph and came across an annoying issue.
The project is configured to use both org.springframework.boot:spring-boot-starter-data-neo4j and org.springframework.data:spring-data-neo4j artifacts (is that ok?)

I have this simple graph: a User is a MEMBER_OF a Group. The MemberOf has a property membershipType. So on the project there are 2 @Node and one @RelationshipProperties classes.


I can easily fetch this by querying:

MATCH (g:Group) <-[m:MEMBER_OF]- (u:User) where u.authId = '123456789123456789' RETURN g,u

The main issue I'm having is on fetching relationship data using spring data neo4j.
I'm trying two different methods and only one of them works.

public interface GroupRepository extends CrudRepository<Group, String> {
..
// #1
List<Group> findAllByMembersUserAuthId(String authId);

// #2  
@Query("MATCH (g:Group) <-[m:MEMBER_OF]- (u:User) where u.authId = $authId RETURN g,u")
List<Group> getByUserAuthId(String authId);
}

When using #1 it's working well and I can see the nice hierarchy of Group->members->User, but when using #2 the members object remains null:
2X_5_501d3107bba7e2f2247b19ce18472d4b13c64d35.png

I played with this a lot and got to a point that members list did have one object, with the membershipType property populated but the TargetNode (User) was still null.

Would really appreciate some help here.

Thanks
Koby

1 REPLY 1

Hi, Koby! Are you still seeing this issue?

I believe you also need to return the relationship entities in your custom query. So it would look something like this:

@Query("MATCH (g:Group) <-[m:MEMBER_OF]- (u:User) where u.authId = $authId RETURN g, collect(m), collect(u);")

I'm also collecting the related entities by the group, as that will ensure you see 1 result per group node (aggregating related entities).

As a side note, with the spring-boot-starter-data-neo4j, you shouldn't need the other dependency because that starter should include the dependencies you need. I'd have to check for sure, but I imagine the other dependency duplicates things already in the starter.

Cheers,
Jennifer