質問

I have successfully incorporated a Play! Framework 2.2.2 application with spring-data-neo4j 3.

I now have to incorporate a simple Java component with neo4j. I'm using neo4j-rest-graphdb-2.0.1 in my new project. Question as follows:

With spring-data-neo4j I had a User model class with @NodeEntity and @TypeAlias("_User") annotations. So I could execute a Cypher query like this:

  @Query("MATCH (User:_User) WHERE User.network = {0} RETURN User")
  Iterable<User> executeFilterTest(String filterValue);

And that would return a list of my User model class objects that I can iterate through.

But now in my Java project I am doing this:

  RestAPI restAPI = new RestAPIFacade("http://localhost:7474/db/data","","");

  CypherResult theResult = restAPI.query("MATCH (User:User) WHERE User.userid = '" + id  + "' RETURN User", new HashMap<String, Object>());

I have no idea how to use the CypherResult? Is there a way I can return the same User model objects as a list like I did in my spring-data-neo4j instance?

役に立ちましたか?

解決

The CypherResult returns cypher-results including nodes and rels as an iterator of maps.

Btw. you should use parameters instead of string concatenation for the id:

RestAPI restAPI = new RestAPIFacade("http://localhost:7474/db/data","","");

...
Map<String, Object> params = new HashMap<String, Object>();
params.put("id",id);
CypherResult theResult = restAPI.query("MATCH (User:User) WHERE User.userid = {id} RETURN User", params);
for (Node user : theResult.to(Node.class)) { 

}

The to() method also takes converters that can create your result objects.

In general I advise to use the JDBC driver instead of Java-Rest-Binding though.

他のヒント

I think you'd have to write your own "mapping" code that takes the result from the CypherResult and turns them into a list of POJOs. I've not done this but looking at the Neo4j REST binding you're using, perhaps it's possible

Spring Data Neo4j was built to facilitate just such a thing. That said, perhaps making use of QueryResult (instead of CypherResult) and one of the "to" overloads/methods (you'd likely have to write your own ResultConverter, etc.). Worst case you'd have to translate the results manually into your POJOs.

This is based on the latest binding, btw.

Again, I've not tried any of this but maybe it's a starting point. I'd love to know what you end up coming up with!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top