Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Spring boot neo4j Repository read query from external properties file

hi guys I need help in the Spring boot neo4j repository

@Query(" MATCH (u:User) WHERE u.userId =~ '(?i)'+$userId RETURN u.userId ")
String userExistsById(@Param("userId") String pStrUserId);

instead of writing query here (inside the interface repo) is there any way can I substitute the query from external properties file

This was a simple query example but I actually had a big query which looks very bad and difficult to understand if I put it in interface file.


Yes, you should be able to do this. I think I would try to use the @Value annotation in Spring. I haven't used that annotation to source in a query before, but theoretically, I think it would still work. Couple links down below for more info on it. Hope this helps!


You can use named queries with Spring Data Neo4j.
This way you just define the method as it is without the query annotation:

interface UserRepository extends Neo4jRepository<User, IdentifierType> {
  User userExistsById(@Param("userId") String pStrUserId);

And define the query in META-INF a file called (default name) where you define the query in this form:

UserRepository.userExistsById="MATCH (u:User) WHERE u.userId =~ '(?i)'+$userId RETURN u.userId"

An example can be found in our test base: