我从Spring Data Neo4j 3迁移到Spring Data Neo4j 4.
我使用Embedded Neo4j数据库。
现在我无法改写以下方法:
public static void cleanDb(Neo4jTemplate template) {
logger.info("Cleaning database");
long deletedNodesCount = 0;
do {
GraphDatabaseService graphDatabaseService = template.getGraphDatabaseService();
Transaction tx = graphDatabaseService.beginTx();
try {
Result<Map<String, Object>> result = template.query("MATCH (n) WITH n LIMIT " + BATCH_SIZE + " OPTIONAL MATCH (n)-[r]-() DELETE n, r RETURN count(n) as count", null);
deletedNodesCount = (long) result.single().get("count");
tx.success();
logger.info("Deleted " + deletedNodesCount + " nodes...");
} catch (Throwable th) {
logger.error("Error while deleting database", th);
throw th;
} finally {
tx.close();
}
} while (deletedNodesCount > 0);
}
如何在SDN4中正确获取graphDatabaseService
并且result.single()
也不存在。
请帮我改写SDN4的这种方法。
答案 0 :(得分:1)
使用EmbeddedDriver时,您可以获得GraphDatabaseService的句柄:
EmbeddedDriver embeddedDriver = (EmbeddedDriver) Components.driver();
GraphDatabaseService databaseService = embeddedDriver.getGraphDatabaseService();
但是,如果您手动管理事务,则可以使用@Transactional或OGM会话中提供的事务方法。