mzober
mzober

Reputation: 91

Neo4JTemplate Create And Get Id in One Query - Spring Data Neo4j 4.0.0.M1

I used in Spring Data Neo4j 3.1.2 this query to create a person and get the id of it:

Create (n:Person {gender:'MALE',sic:'sic-123'}) return id(n)

with my Neo4JTemplate i execute this query with:

neo4JTemplate.query("Create (n:Person {gender:{gender},sic:{sic}}) "+ 
"return id(n)",ImmutableMap.of("gender","MALE","sic","sic-123");

Now i want to use Spring Data Neo4J 4.0.0.M1, because of it's server-support.

In SDN 4 queries can executed with the Neo4jTemplate by

void neo4jTemplate.execute(String jsonStatement) 
//CREATE, MERGE or DELETE the node

or

<T> T queryForObject(Class<T> objectType, String cypher, Map<String,?> parameters) 
//The RETURN of an Object possible but read only - mode (no creation)

<T> Iterable<T> queryForObjects(Class<T> objectType, String cypher,
Map<String,?> parameters) 

//The RETURN of Objects possible but read only - mode (no creation)


Iterable<Map<String,Object>> query(String cypher,
Map<String,?> parameters)   
//The RETURN of Objects possible but read only - mode (no creation)

My Question now is: how can i use the neo4jTemplate to use only ONE query that create my node and get me the id of it?

P.S.: The creation and return of the id works, but i want only one query to manage this problem.

Upvotes: 1

Views: 814

Answers (1)

Luanne
Luanne

Reputation: 19373

At the moment you can't execute this sort of query using the Neo4jTemplate. Only non modifying Cypher queries are allowed to return results.

You'd have to save the Person entity and then use the id that's been populated after save.

This is scheduled to be fixed (though the fix version is not guaranteed)- see https://github.com/neo4j/neo4j-ogm/issues/22

Upvotes: 1

Related Questions