Search code examples
neo4j-ogm

Filter on relationship's field


I'm trying to fetch all entities for a given relationship's field match (I want my entity's relationships filled out in the result). Trying with Filter on session.loadAll() to filter on the relationship's field but I can't make it work.

My entities definition looks like:

@NodeEntity    
class ClockAction {
 @Id @GeneratedValue
 private Long id;
 private String description
 private User user;
 private Office office;
}

@NodeEntity    
class User {
 @Id @GeneratedValue
 private Long id;
 private String name;
 private List<ClockAction> clockActions;
}

@NodeEntity    
class Office {
 @Id @GeneratedValue
 private Long id;
 private String name;
 private List<ClockAction> clockActions;
}

From that I'm need to retrieve all ClockAction entities where User.id is in a given set of Ids.

Here is my try :

Filter filter = Filter("id", ComparisonOperator.IN, userIds);
filter.setNestedPropertyName("user");
filter.setNestedPropertyType(User.class);
filter.setNestedRelationshipEntity(true);

return session.loadAll(ClockAction.class, filter);

This always returns an empty result. Any idea of what I'm doing wrong?

Using a session.query like this

session.query(ClockAction.class, "MATCH p=(a:ClockAction)-[r]-() WHERE id(r) IN {ids} RETURN nodes(p), rels(p), a, r ORDER BY a.id", params) 

works but only office field of ClockAction gets filled out on the result entity, user is always null...

Any help is appreciated :)


Solution

  • I ended up following advises from @meistermeier and annotate my relationships giving direction.

    Below is my model entities :

    @NodeEntity    
    class ClockAction {
     @Id @GeneratedValue
     private Long id;
     private String description
     @Relationship(direction = Relationship.OUTGOING)
     private User user;
     @Relationship(direction = Relationship.OUTGOING)
     private Office office;
    }
    
    @NodeEntity    
    class User {
     @Id @GeneratedValue
     private Long id;
     private String name;
     @Relationship(direction = Relationship.INCOMING)
     private List<ClockAction> clockActions;
    }
    
    @NodeEntity    
    class Office {
     @Id @GeneratedValue
     private Long id;
     private String name;
     @Relationship(direction = Relationship.INCOMING)
     private List<ClockAction> clockActions;
    }
    

    What @meistermeier suggested for query did not work for me, but gave me inspiration and I found this working fine :

    MATCH p((u:User)-[ur]-(c:ClockAction)-[or]-()) WHERE id(u) IN {ids} RETURN p, rels(p)