suksant
suksant

Reputation: 43

Passing parameters to cypher query in @Query in Spring Data Neo4J

I have a User domain which: 1) String username, 2) String password, 3) Roles [] roles (Roles is enum).

A method in my UserRepository is as follows:

@Query("MERGE (n:User:_User { username: \"{0}.username\", password: \"{0}.password\", roles: \"{0}.roles\" }) RETURN n")
User registerUser(User user);

This throws org.springframework.core.convert.ConversionFailedException as it cannot convert String to Roles, which is an enum. If I get rid of the quotes around {0}.roles then it will throw this:

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is org.neo4j.helpers.ThisShouldNotHappenError: Developer: Andres claims that: Need something with properties

It would be ideal if the User argument can be serialized directly into a MERGE query or any way to parse this Roles [].

Can anyone help me with this please? I was using SDN 3.1.0 and Neo4J 2.0.3.

Update1: Ideally, it'd be great to do something like:

@Query("MERGE (n:User:_User {0}) RETURN n")
User registerUser(User user);

Upvotes: 4

Views: 4829

Answers (2)

Anush B M
Anush B M

Reputation: 91

This actually worked for me.

Instead of passing the argument location, passing the argument reference name works.

@Query("MERGE (n:User:_User { username: {name}, password: {password}, roles: {roles} }) RETURN n")
User registerUser(String name, String password, Collection<String> roles);

Upvotes: 1

Michael Hunger
Michael Hunger

Reputation: 41676

I'm not sure how that would be supported. It would have to convert the User into a map to be used as parameter, usually when you pass in an entity it is converted into the internal node-id for that entity.

Why don't you just do template.save(user) ?

You can try to pass in the parameters individually.

@Query("MERGE (n:User:_User { username: {0}, password: {1}, roles: {2} }) RETURN n")
User registerUser(String name, String password, Collection<String> roles);

Upvotes: 3

Related Questions