我有一个奇怪的问题,我可以使用createRelationshipTo()方法在嵌入式neo4j中创建关系。但是,当我使用getRelationships()方法访问这些关系时,找不到任何关系。当我创建关系并立即使用getRelationships()方法时,找到该关系。但是后来,这种关系就失去了。
E.g。在sampleMethod()中,我在同一个方法中创建和检索关系,因此它返回两个关系,但是当我尝试使用sampleMethod2()中相同的getRelationships()方法检索相同的关系时,它什么都没有。
public ArrayList<String> sampleMethod() {
ArrayList<String>list = new ArrayList<String>();
DatabaseConnection dbConnection = new DatabaseConnection();
GraphDatabaseService graphDb = dbConnection.connectToDatabase();
Transaction tx = graphDb.beginTx();
Node user = graphDb.findNode(Label.label("User"), "userName", "hamna123");
Node restaurant = graphDb.findNode(Label.label("Restaurants"), "name", "The Grand Regency");
Relationship rel = user.createRelationshipTo(restaurant, RelationshipType.withName("Visited"));
Relationship rel2 = user.createRelationshipTo(restaurant, RelationshipType.withName("Favorite"));
for (Relationship relationship : user.getRelationships()) {
String found = relationship.getType().name().toString();
list.add(found);
}
tx.close();
//graphDb.shutdown();
return list;
}
public ArrayList<String> sampleMethod2() {
ArrayList<String>list = new ArrayList<String>();
DatabaseConnection dbConnection = new DatabaseConnection();
GraphDatabaseService graphDb = dbConnection.connectToDatabase();
Transaction tx = graphDb.beginTx();
Node user = graphDb.findNode(Label.label("User"), "userName", "hamna123");
Iterable<Relationship> relations = user.getRelationships();
for (Relationship relationship : relations) {
String found = relationship.getType().name().toString();
list.add(found);
}
tx.close();
graphDb.shutdown();
return list;
}
答案 0 :(得分:0)
不要在没有提交的情况下关闭交易。否则,将执行回滚。
所以:
catch-with-resource
模式关闭交易示例:
try ( Transaction tx = db.beginTx())
{
Node myNode = db.createNode();
myNode.setProperty( "name", "my node" );
tx.success(); // commit the tx
}