cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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.

2 REPLIES 2

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!

https://www.baeldung.com/spring-value-annotation

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/a...

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 neo4j-named-queries.properties (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:

https://github.com/spring-projects/spring-data-neo4j/blob/ee60a1faf643f3d033c1d40eb3b3ed527bf3cb46/s...
https://github.com/spring-projects/spring-data-neo4j/blob/78bb07356bd6d646bbc08ad1adcb1509c51ab655/s...