我最近开始探索图形数据库(特别是 Neo4j 和 OrientDB),并且遇到了一个我似乎无法解决的问题。
我正在运行 OrientDB 的本地安装(OrientDB Server v2.0-M3 处于活动状态。)。我正在使用 Tinkerpops 连接到图表并针对图表运行查询。我在本地 Tomcat 7 服务器上使用 Java 和 Spring。测试我的 API 我在 Chrome 上使用 Postman。
这是我错误的 GET 方法:
@RequestMapping(value = "/articles", method = RequestMethod.GET)
public
@ResponseBody
Vector<Article> list() {
OrientGraph graph = new OrientGraph("remote:/local/path/to/orientdb/databases/mydb", "user", "password");
FramedGraphFactory factory = new FramedGraphFactory();
FramedGraph manager = factory.create(graph);
Vector<Article> articles = new Vector<>();
try {
Iterable<Vertex> vertices = graph.getVerticesOfClass("Article", false);
Iterator<Vertex> it = vertices.iterator();
if (it.hasNext()) {
do {
Article a = (Article) manager.frame(it.next(), Article.class);
articles.add(a);
} while (it.hasNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
graph.shutdown();
}
return articles;
}
这会产生以下错误:
{
"timestamp": 1418562889304,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write JSON: Database instance is not set in current thread. Assure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db); (through reference chain: java.util.Vector[0]->$Proxy43[\"name\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Database instance is not set in current thread. Assure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db); (through reference chain: java.util.Vector[0]->$Proxy43[\"name\"])",
"path": "/articles"
}
我一直试图解决这个问题,尝试错误提示的“修复”。我也尝试使用 TransactionalGraph 而不是 OrientGraph。
这是关键......我也在使用类似的方法来获取单个资源。此方法仅在我使用“System.out.println”时才有效,否则它会失败并出现相同的错误。
@RequestMapping(value = "/article", method = RequestMethod.GET)
public
@ResponseBody
Article get(
@RequestParam(value = "number", required = true) long number
) {
TransactionalGraph graph = new OrientGraph("remote:/path/to/local/orientdb/orientdb/databases/mydb", "user", "password");
FramedGraphFactory factory = new FramedGraphFactory();
FramedGraph manager = factory.create(graph);
Article article = null;
try {
Iterable<Article> articles = manager.getVertices("number", number, Article.class);
article = articles.iterator().next();
System.out.println(article.getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
graph.shutdown();
}
return article;
}
任何帮助表示赞赏!