I'm having issues getting a neo4j RelationshipEntity persisted with Spring Boot. I'm using spring-boot-starter-data-neo4j (2.1.0.RELEASE), and the neo4j docker image tagged 3.4.9.
I have a simple NodeEntity, which contains a collection for the RelationshipEntity:
@NodeEntity
public class Book {
@Id
@GeneratedValue
private Long id;
private String name;
public Book() {}
public Book(String name) {
this.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:
@NodeEntity
public class CreditCard {
@Id
@GeneratedValue
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 {
@Id
@GeneratedValue
private long id;
@DateString("yyyy-MM-dd")
Date purchaseDate;
@StartNode
private Book book;
@EndNode
private CreditCard card;
public BookPurchase(){}
public BookPurchase(CreditCard card, Book book, Date purchaseDate) {
this.card = card;
this.book = book;
this.purchaseDate = purchaseDate;
this.card.getPurchases().add(this);
this.book.getPurchases().add(this);
}
// getters and setters follow
}
And finally I have the Spring controller tying everything together:
@RestController
public class ExamplesController {
@Autowired
CreditCardRepository creditCardRepository;
@PostMapping(value="/purchases")
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());
creditCardRepository.save(card);
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?
MATCH (n) OPTIONAL MATCH ()-[r]->() RETURN *
return? Your shown query only returns nodes, and by default (depending on Neo4j version), auto relation linking in the browser is turned off (and you didn't say if you clicked the expand relationships button at the bottom of the circle of options) – Tezra