Search code examples
springneo4jspring-data-neo4jspring-data-neo4j-4neo4j-ogm

How to save relationship in relationship entity?


I have two class first is Company :

@GraphId
private Long graphId;

private String id;

private String name;

Second class is Product :

@GraphId
private Long graphId;

private String id;

private String name;

Between those two class the relationship is company has license for and i want to create relationship entity so I can save the property in the relationship. So i create this relationship entity class called CompanyHasLicenseFor :

@RelationshipEntity(type="company_has_license_for")
public class CompanyHasLicenseFor {
    @GraphId
    private Long graphId;

    private String id;

    @StartNode
    private Company company;

    @EndNode
    private Product product;

    private Date startDate;

    private Date endDate;

}

This is the code i use to create the relationship :

Company company = new Company("Company Test");
companyService.save(company);
Product product = new Product("Product Test");
productService.save(product);
CompanyHasLicenseFor com = new CompanyHasLicenseFor(company, product, new Date(), new Date());
companyHasLicenseForService.save(com);

When i try to create the dummy data using the code above the relationship between two nodes didn't created. Only the node company and product are created. How to create/persist the relationship entity in this case ?

Thank you

Update #1:

I already tried to add CompanyHasLicenseFor as a relationship property in my company class : @GraphId private Long graphId;

private String id;

private String name;

@Relationship(type="company_has_license_for")
private CompanyHasLicenseFor companylicense;

Yet the relationship still not created

Update #2:

@NodeEntity
public class Company {
    @GraphId
    Long graphId;
    String id;
    String name;

    @Relationship(type="company_has_license_for", direction = "UNDIRECTED")
    Set<CompanyHasLicenseFor> companylicenses;

    .....
}

@NodeEntity
public class Product {
    @GraphId
    Long graphId;
    String id;
    String name;

    @Relationship(type = "company_has_license_for", direction = "UNDIRECTED")
    Set<CompanyHasLicenseFor> companylicenses;

    ....    
}

@RelationshipEntity(type="company_has_license_for")
public class CompanyHasLicenseFor {
    @GraphId
    Long graphId;
    String id;

    @StartNode
    Company company;

    @EndNode
    Product product;

    ....
}

I'm using Spring Data Neo4j 4.2.0.RELEASE and neo4j-ogm-2.1.1

This is my Sample Code if needed

Screenshot of my database


Solution

  • Answer 1 :

    I look at your Sample Project. The problem is, you are not define the neo4jconfig package of your relationship model. You need to define your package of com.example.neo.relationshipModel at SessionFactory like this:

    @Bean
    public SessionFactory sessionFactory() {
        // with domain entity base package(s)
        return new SessionFactory("com.example.neo.model","com.example.neo.relationshipModel", "BOOT-INF.classes.com.example.neo.model");
    }
    

    Answer 2 :

    If you don't want to change your neo4jconfig, You can change your model structure, move the CompanyHasLicenseFor.java to package com.example.neo.model like this :

    enter image description here