我在使用Neo Boot保持neo4j RelationshipEntity时遇到问题。我正在使用spring-boot-starter-data-neo4j(2.1.0.RELEASE),并且neo4j docker镜像标记为3.4.9。
我有一个简单的NodeEntity,其中包含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
}
我有另一个NodeEntity,它也包含关系实体的集合:
@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
}
我有RelationshipEntity,它在构造函数中添加了对两个NodeEntity类的引用:
@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
}
最后,我让Spring控制器将所有内容捆绑在一起:
@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";
}
}
每当我尝试curl -X POST http://localhost:8080/purchases
时,我只会在neo4j浏览器中看到以下内容-RelationshipEntity不会持久化,只会持久化节点。
有人可以协助吗?
答案 0 :(得分:0)
感谢Gerrit Meier回答了这一问题。我的RelationshipEntity使用的是原语long
而不是对象/包装器Long
。这里的完整详细信息:https://community.neo4j.com/t/neo4j-relationshipentity-not-persisted/3039