1
votes

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

2
Are companyService, productService and companyHasLicenseForService are all Spring Data Repositorys? If so it should all work. You will probably need a @Transactional surrounding your three save methods if you want that action to be atomic.digx1
@digx1 Yes all of the service will call the repository function. I already put Transactional annotation above the public class declaration. But the relationship still not created in neo4jIwan Ivanto

2 Answers

0
votes

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

0
votes

From the SDN docs :

paths in the graph are written starting with nodes first and then relationships are created between them. Therefore, you need to structure your domain models so that relationship entities are reachable from node entities for this to work correctly

So I guess you need to make CompanyHasLicenseFor an attribute of your entities.

Update after looking at the sample code :

AND

You should ensure both your entities and relationships are scanned by the OGM. In your config (session factory init), you tell OGM to scan package com.example.neo.model but your relationship entity lives in package com.example.neo.relationshipModel, so it is not picked up.

=> Move CompanyHasLicenseFor class in the scanned package or add the other package in the session factory constructor.