Search code examples
springneo4jspring-data-neo4j-4

Neo4j RelationshipEntity StackOverflow


I'm having trouble understanding how the @RelationshipEntity works. I've tried following examples, but even though I think I'm following the same pattern as the example, I end up witha stackoverflow, because the Relationship Entity grabs the NodeEntity, which has the RelationshipEntity, and on and on...

My model is: (:Vendor)-[:BELONGS_TO {active: true, sinceDate: date}]->(:Store)

So my two nodes are Vendor and Store:

@NodeEntity
@Data
public class Vendor {

    @GraphId
    private Long id;

    private Long vendorId;

    private String name;

    private String address;

    @Relationship(type = "OWNS")
    private Collection<Inventory> inventory;

    @Relationship(type = "BELONGS_TO")
    private Collection<Store> store;
}

@NodeEntity
@Data
public class Store {

    @GraphId
    private Long id;

    private Long storeId;

    private String name;

    private String address;

    private String email;

    @Relationship(type = "BELONGS_TO", direction = Relationship.INCOMING)
    private List<StoreParticipant> storeParticipant;
}

And my RelationshipEntity:

@RelationshipEntity(type = "BELONGS_TO")
@Data
public class StoreParticipant {

    @GraphId
    private Long id;

    @StartNode
    private Vendor vendor;

    @EndNode
    private Store store;

    private int count;

    private double price;

    private boolean negotiable;

    private boolean active;
}

I based this off of the Movie example which had (:Person)-[:ACTED_IN]->(:MOVIE) and the acted_in relationship was ROLE

This is happening when I call the repository method findByVendorId

@Repository
public interface VendorRepository extends GraphRepository<Vendor> {
    List<Vendor> findByVendorId(Long vendorId);
}

Solution

  • If you're referencing this from both ends, you need to reference the relationship entity, not the node entity directly.

    Store looks fine but Vendor contains

     @Relationship(type = "BELONGS_TO")
     private Collection<Store> store;
    

    when it should be

     @Relationship(type = "BELONGS_TO")
     private Collection<StoreParticipant> store;