Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Query by relationship properties

Hi all,

I am using Spring Data Neo4J 6.1.2 and would like to retrieve node entities by a relationship property. I am currently using dynamic relationships.

On the node entity I have this:

public class Entity {

    private String id;
    private Map<String, List<RelatedEntity>> relatedEntities = new HashMap<>();

On the relationship I have the following properties:

public class RelatedEntity {

    private Long id;

    private Entity relEntity;    

    private String createdBy;

I want to create repository methods to retrieve entities by the id and created by field. I have tried these:

List<Entity> findByRelatedEntitiesCreatedBy(String createdBy);
List<Entity> findByRelatedEntitiesId(Long id);

But I get this exception:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityNeo4jRepository' defined in company.graph.repository.EntityNeo4jRepository defined in @EnableNeo4jRepositories declared on Neo4jRepositoriesRegistrar.EnableNeo4jRepositoriesConfiguration: Invocation of init method failed; nested exception is Could not create query for public abstract java.util.List company.graph.repository.EntityNeo4jRepository.findByRelatedEntitiesCreatedBy(java.lang.String)! Reason: No property createdBy found for type List! Traversed path: Entity.relatedEntities.; nested exception is No property createdBy found for type List! Traversed path: Entity.relatedEntities.

What am I doing wrong?


Node Clone

You can try writing a custom query using the @Query annotation for this method and try extracting the entity list result. In this way it would give more control over the response of the query and give more flexibility in terms of what you want to query.



For dynamic relationships this is not possible. The reason is that path the query findByRelatedEntitiesCreatedBy takes (relatedEntities.createdBy) just can see the value type of the map (List) but not the generic type within this list.
This is why you are getting the:

No property createdBy found for type List!

exception message.
There is currently no option to add this feature in Spring Data Neo4j. You would have to define a custom @Query functions with a parameter for the createdBy value as @dhruv.sharma8826 suggested.

OK thanks Gerrit, Dhruv.

I'll work around it and add custom query methods.