Neo4j RelationshipEntity not persisted

(Shane) #1

Hi everyone, I'm having some issues getting a neo4j RelationshipEntity persisted with neo4j and Spring Boot.

I have a simple NodeEntity, which contains a collection for the RelationshipEntity:

public class Book {
    private Long id;
    private String name;

    public Book() {}

    public Book(String name) { = name;

    @Relationship(type = "PURCHASED_WITH", direction = "OUTGOING")
    private Set<BookPurchase> purchases = new HashSet<>();

    // getters and setters follow

I have another NodeEntity, which also contains a collection for the relationship entity:

public class CreditCard {

    private Long id;
    private String number;

    @DateString(value = "yyyy-MM-dd")
    private Date expiryDate;

    public CreditCard() {}

    public CreditCard(String number, Date expiryDate) {
        this.number = number;
        this.expiryDate = expiryDate;

    @Relationship(type = "PURCHASED_WITH", direction = "INCOMING")
    private Set<BookPurchase> purchases = new HashSet<BookPurchase>(); 

    // getters and setters follow

I have the RelationshipEntity, which adds references to both NodeEntity classes in the constructor:

@RelationshipEntity(type = "PURCHASED_WITH")
public class BookPurchase {
    private long id;

    Date purchaseDate;

    private Book book;

    private CreditCard card;

    public BookPurchase(){}

    public BookPurchase(CreditCard card, Book book, Date purchaseDate) {
        this.card = card; = book;
        this.purchaseDate = purchaseDate;

    // getters and setters follow

And finally I have the Spring controller tying everything together:

public class ExamplesController {

    CreditCardRepository creditCardRepository;

    public String createPurchases() {
        CreditCard card = new CreditCard("11111", new GregorianCalendar(2018, Calendar.FEBRUARY, 12).getTime());

        Book book1 = new Book("of mice and men");
        BookPurchase purchase1 = new BookPurchase(card,book1,new GregorianCalendar(2018, Calendar.MARCH, 15).getTime());;
        return "Successfully created entities";

Whenever I try to curl -X POST http://localhost:8080/purchases , I just see the following in the neo4j browser - the RelationshipEntity is not persisted, only the nodes.

Can anyone assist?

(Gerrit Meier) #2

The problem is that you are using a long instead of the object/wrapper type Long in your BookPurchase. Neo4j-OGM assumes that the relationship entity already exists in the database because it has a non-null value (primitive long defaults to 0).
Using the Long type will fix your problem.

(Shane) #3

Thanks very much @gerrit.meier, that was exactly it!

(Gerrit Meier) #4

Could you please set the solved checkmark? It helps others to navigate better to a solution.