Search code examples
neo4jspring-data-neo4j-4

spring data neo4j @Query by relationship type


I need to query neo4j by relationship type

this is My Entity class

@NodeEntity
@Getter
@Setter
@NoArgsConstructor
public class ProductRecommendation {
    @GraphId
    private Long id;

    String product;
    @Relationship(type = "RECOMMENDS", direction = Relationship.OUTGOING)
    Set<ProductRecommendation> linkedProducts = new HashSet<>();
}

I need to find all the nodes with relationship type as "RECOMMENDS".
Is there a default findBy method?
I tried with this and it works

public interface ProductRecommendationRepository extends GraphRepository<ProductRecommendation> {
    @Query("MATCH p=()-[r:RECOMMENDS]->() RETURN p")
    List<ProductRecommendation> findByRelationShipType();
}

however if I pass relationship type as variable, it doesn't work

public interface ProductRecommendationRepository extends GraphRepository<ProductRecommendation> {
    @Query("MATCH p=()-[r:{0}]->() RETURN p")
    List<ProductRecommendation> findByRelationShipType(String type);
}

Can someone please explain.

Thanks


Solution

  • The relationship type cannot be parameterised (see http://neo4j.com/docs/developer-manual/current/cypher/#cypher-parameters).

    so you will have to go with

    public interface ProductRecommendationRepository extends GraphRepository<ProductRecommendation> { @Query("MATCH p=()-[r:RECOMMENDS]->() RETURN p") List<ProductRecommendation> findByRelationShipType(); }