neo4j空间代码中的类强制转换异常

时间:2014-05-27 15:29:31

标签: neo4j-spatial

以下代码段:

GraphDatabaseService graphDb = new EmbeddedGraphDatabase("var/geo");
// Wrap it as a spatial db service
SpatialDatabaseService spatialDb = new SpatialDatabaseService(graphDb);
// Create the layer to store our spatial data
EditableLayer runningLayer = (EditableLayer) spatialDb.getOrCreateLayer("running", SimplePointEncoder.class, EditableLayerImpl.class, "lon:lat");

因错误而失败:

Exception in thread "main" java.lang.ClassCastException: org.neo4j.collections.graphdb.impl.EmbeddedGraphDatabase cannot be cast to org.neo4j.kernel.GraphDatabaseAPI
at org.neo4j.cypher.ExecutionEngine.<init>(ExecutionEngine.scala:113)
at org.neo4j.cypher.javacompat.ExecutionEngine.<init>(ExecutionEngine.java:53)
at org.neo4j.cypher.javacompat.ExecutionEngine.<init>(ExecutionEngine.java:43)
at org.neo4j.collections.graphdb.ReferenceNodes.getReferenceNode(ReferenceNodes.java:60)
at org.neo4j.gis.spatial.SpatialDatabaseService.getSpatialRoot(SpatialDatabaseService.java:76)
at org.neo4j.gis.spatial.SpatialDatabaseService.getLayer(SpatialDatabaseService.java:108)
at org.neo4j.gis.spatial.SpatialDatabaseService.getOrCreateLayer(SpatialDatabaseService.java:202)
at com.bmt.contain.spatial.test.SpatialTest.main(SpatialTest.java:47)

我试图让here的示例代码正常工作,我已经在下面包含了相关的import语句,以防我以某种方式导入错误版本的函数。

import org.neo4j.collections.graphdb.impl.EmbeddedGraphDatabase;

import org.neo4j.graphdb.GraphDatabaseService;

import org.neo4j.gis.spatial.EditableLayer;
import org.neo4j.gis.spatial.EditableLayerImpl;
import org.neo4j.gis.spatial.Layer;
import org.neo4j.gis.spatial.encoders.SimplePointEncoder;

有人可以告诉我吗?

此外,neo4j和v13的空间为2.0.1。

<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-spatial</artifactId>
    <version>0.13-neo4j-2.0.1</version>
</dependency>
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j</artifactId>
    <version>2.0.1</version>
</dependency>

1 个答案:

答案 0 :(得分:0)

所以这个问题的答案是你需要使用

GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase("var/geo");

而不是

GraphDatabaseService graphDb = new EmbeddedGraphDatabase("var/geo");

在幕后的某个地方,这会创建一种不同类型的嵌入式GDbS,它不会导致类强制转换异常。