4

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);
suksant
  • 43
  • 1
  • 4

2 Answers2

3

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);
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • 1
    Thanks a lot Michael! I did try passing the parameters individually and it worked - but that will mean a change every time we modify the domain. template.save(user) does a MERGE to existing node in the graph. I want to achieve something similar to CREATE UNIQUE with MERGE as an additional call to check whether a node exists would be redundant. Should I just use CREATE UNIQUE instead (the doc seems to recommend using MERGE instead from 2.x onwards)? – suksant Jul 11 '14 at 08:12
  • 1
    So we can't use properties of a pojo param inside the query? I want to do similar to {0}.username – Skystrider Nov 06 '15 at 14:21
1

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);
Anush B M
  • 91
  • 1
  • 6