Spring Data Rest Neo4j - Querying Entities
I have been using Spring Data Rest and trying out the Neo4J implementation. This was going well but as it was on Build Snapshots, and now on Milestone 1, things change.
The first thing to note, as it currently stands, it is not as easy as it should be to create query methods in the repository. For example:
Member findByName(@Param("name") String name);
This will not work as expected, the error returned is:
org.neo4j.cypher.ParameterNotFoundException: Expected a parameter named 0
So after much searching on forums, looking through documentation and example code, there was nothing that would help with this issue. So late one night, I had a brainwave to try something. I created a custom Cypher query to return the data I needed.
@Query("start member=node:Member(name={name}) return member") Member findByName(@Param("name") String name);
Above you can see that in the @Query, I have a parameter to be injected called name, and the param on the request is also called name. After this update, alas!, everything worked as expected.
After all of this I thought all of my troubles were behind me, but Spring Data changed something in the core and nothing worked any more, there was an error for a MissingIndexException. It was saying that "Member" was not Indexed.
Again, more forum searching, more documentation reading, more trawling through example code and trying to guess what the correct code would be, there were no answers for this issue again!
While reading through the documentation I was reading about the different types of Repositories that were available to Spring Data Neo4J and seen that IndexRepository and NamedIndexRepository were available. So I replaced GraphRepository with both of these, doing this caused the repositories to disappear. I then had an idea to to extend more than one repository:
public interface MemberRepository extends GraphRepository, NamedIndexRepository
The entity class now looks like:
@NodeEntity public class Member { @GraphId private Long id; @Indexed("name-search") private String name; //Getters and Setters }
And now everything is hunky dory again. The search works as expected.
I know what it's like coming up against all of these issues when using a new technology, so thanks to a friend who persuaded me to write this blog, I hope this is useful to someone, somewhere, someday.
So, until the next time something changes in Spring Data Neo4J or Spring Data Rest...