Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 988
suksant Avatar asked Oct 23 '25 17:10

suksant


2 Answers

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);
like image 102
Michael Hunger Avatar answered Oct 26 '25 11:10

Michael Hunger


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);
like image 25
Anush B M Avatar answered Oct 26 '25 12:10

Anush B M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!