新手到SDN和Neo4j。使用sdn版本:4.1.6.RELEASE)和neo4j版本:3.1.0。
我正在尝试一种使用Neo4jTemplate持久保存对象的简单编程方法,而不需要任何存储库支持,但它似乎不起作用。
我的代码(独立应用):
public class Scratchpad {
public static void main(String[] args) throws Exception {
Configuration config = new Configuration();
config.driverConfiguration()
.setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
.setCredentials("neo4j", "xxxx")
.setURI("http://localhost:7474");
System.out.println(config);
SessionFactory sf = new SessionFactory(config, "domain");
Session session = sf.openSession();
final Neo4jTemplate neo4jTemplate = new Neo4jTemplate(session);
PlatformTransactionManager pt = new Neo4jTransactionManager(session);
final TransactionTemplate transactionTemplate = new TransactionTemplate(pt);
transactionTemplate.execute((TransactionCallback<Object>) transactionStatus -> {
Person p = new Person("Jim", 1);
p.worksWith(new Person("Jack", 2));
p.worksWith(new Person("Jane", 3));
neo4jTemplate.save(p, 2);
return p;
});
}
}
我的实体(显示在域包中)如下所示:
@NodeEntity
public class Person {
@GraphId
private Long id;
private String name;
private Person() {
// Empty constructor required as of Neo4j API 2.0.5
}
;
public Person(String name, long id) {
this.id = id;
this.name = name;
}
/**
* Neo4j doesn't REALLY have bi-directional relationships. It just means when querying
* to ignore the direction of the relationship.
* https://dzone.com/articles/modelling-data-neo4j
*/
@Relationship(type = "TEAMMATE", direction = Relationship.UNDIRECTED)
public Set<Person> teammates;
public void worksWith(Person person) {
if (teammates == null) {
teammates = new HashSet<>();
}
teammates.add(person);
}
public String toString() {
return this.name + "'s teammates => "
+ Optional.ofNullable(this.teammates).orElse(
Collections.emptySet()).stream().map(
person -> person.getName()).collect(Collectors.toList());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
日志中没有出现错误的迹象。但是当我使用Web控制台查询Neo4J时,没有节点存在。
答案 0 :(得分:2)