Spring Data Neo4j 4 GraphDatabaseService和result.single()

时间:2016-05-24 09:12:41

标签: neo4j spring-data-neo4j-4 neo4j-ogm

我从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的这种方法。

1 个答案:

答案 0 :(得分:1)

使用EmbeddedDriver时,您可以获得GraphDatabaseService的句柄:

 EmbeddedDriver embeddedDriver = (EmbeddedDriver) Components.driver();
 GraphDatabaseService databaseService = embeddedDriver.getGraphDatabaseService();

但是,如果您手动管理事务,则可以使用@Transactional或OGM会话中提供的事务方法。