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:

@Node("Entity")
@Data
public class Entity {

    @Id
    @GeneratedValue(UUIDStringGenerator.class)
    private String id;
...
    @Relationship
    private Map<String, List<RelatedEntity>> relatedEntities = new HashMap<>();

On the relationship I have the following properties:

@RelationshipProperties
@Data
public class RelatedEntity {

    @Id
    @GeneratedValue()
    private Long id;

    @TargetNode
    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 org.springframework.data.repository.query.QueryCreationException: 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 org.springframework.data.mapping.PropertyReferenceException: No property createdBy found for type List! Traversed path: Entity.relatedEntities.

What am I doing wrong?

Hi,
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.

Regards.

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.