|
| 1 | +# Spring Data FalkorDB Annotations |
| 2 | + |
| 3 | +This document describes the annotations available in Spring Data FalkorDB for mapping entities and defining custom queries. |
| 4 | + |
| 5 | +## Query Annotations |
| 6 | + |
| 7 | +### @Query |
| 8 | + |
| 9 | +The `@Query` annotation allows you to define custom Cypher queries for repository methods that cannot be expressed as derived queries. |
| 10 | + |
| 11 | +**Location:** `org.springframework.data.falkordb.repository.query.Query` |
| 12 | + |
| 13 | +#### Basic Usage |
| 14 | + |
| 15 | +```java |
| 16 | +public interface UserRepository extends FalkorDBRepository<User, Long> { |
| 17 | + |
| 18 | + @Query("MATCH (u:User) WHERE u.age > $age RETURN u") |
| 19 | + List<User> findUsersOlderThan(@Param("age") int age); |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +#### Parameter Binding |
| 24 | + |
| 25 | +The `@Query` annotation supports multiple parameter binding techniques: |
| 26 | + |
| 27 | +1. **By parameter name using @Param:** |
| 28 | + ```java |
| 29 | + @Query("MATCH (u:User) WHERE u.name = $name RETURN u") |
| 30 | + User findByName(@Param("name") String name); |
| 31 | + ``` |
| 32 | + |
| 33 | +2. **By parameter index:** |
| 34 | + ```java |
| 35 | + @Query("MATCH (u:User) WHERE u.age > $0 RETURN u") |
| 36 | + List<User> findUsersOlderThan(int age); |
| 37 | + ``` |
| 38 | + |
| 39 | +3. **By entity property:** |
| 40 | + ```java |
| 41 | + @Query("MATCH (u:User {id: $user.__id__})-[:FOLLOWS]->(f) RETURN f") |
| 42 | + List<User> findFollowing(@Param("user") User user); |
| 43 | + ``` |
| 44 | + |
| 45 | +#### Query Types |
| 46 | + |
| 47 | +**Count Queries:** |
| 48 | +```java |
| 49 | +@Query(value = "MATCH (u:User) WHERE u.age > $age RETURN count(u)", count = true) |
| 50 | +Long countUsersOlderThan(@Param("age") int age); |
| 51 | +``` |
| 52 | + |
| 53 | +**Exists Queries:** |
| 54 | +```java |
| 55 | +@Query(value = "MATCH (u:User {name: $name}) RETURN count(u) > 0", exists = true) |
| 56 | +Boolean existsByName(@Param("name") String name); |
| 57 | +``` |
| 58 | + |
| 59 | +**Write Operations:** |
| 60 | +```java |
| 61 | +@Query(value = "MATCH (u:User {id: $id}) SET u.lastLogin = timestamp() RETURN u", write = true) |
| 62 | +User updateLastLogin(@Param("id") Long id); |
| 63 | +``` |
| 64 | + |
| 65 | +#### Complex Queries |
| 66 | + |
| 67 | +```java |
| 68 | +@Query("MATCH (m:Movie)-[r:ACTED_IN]-(p:Person {name: $actorName}) " + |
| 69 | + "RETURN m, collect(r), collect(p)") |
| 70 | +List<Movie> findMoviesByActorName(@Param("actorName") String actorName); |
| 71 | +``` |
| 72 | + |
| 73 | +## Relationship Mapping Annotations |
| 74 | + |
| 75 | +### @TargetNode |
| 76 | + |
| 77 | +The `@TargetNode` annotation marks a field in a relationship properties class as the target node of the relationship. |
| 78 | + |
| 79 | +**Location:** `org.springframework.data.falkordb.core.schema.TargetNode` |
| 80 | + |
| 81 | +#### Usage with @RelationshipProperties |
| 82 | + |
| 83 | +```java |
| 84 | +@RelationshipProperties |
| 85 | +public class ActedIn { |
| 86 | + |
| 87 | + @RelationshipId |
| 88 | + private Long id; |
| 89 | + |
| 90 | + @TargetNode |
| 91 | + private Person actor; // The target node of the relationship |
| 92 | + |
| 93 | + private List<String> roles; // Relationship properties |
| 94 | + private Integer year; // Relationship properties |
| 95 | + |
| 96 | + // constructors, getters, setters... |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +#### Complete Example |
| 101 | + |
| 102 | +**Entity Classes:** |
| 103 | +```java |
| 104 | +@Node |
| 105 | +public class Movie { |
| 106 | + @Id |
| 107 | + private String title; |
| 108 | + |
| 109 | + @Relationship(type = "ACTED_IN", direction = Direction.INCOMING) |
| 110 | + private List<ActedIn> actors = new ArrayList<>(); |
| 111 | + |
| 112 | + // other fields... |
| 113 | +} |
| 114 | + |
| 115 | +@Node |
| 116 | +public class Person { |
| 117 | + @Id @GeneratedValue |
| 118 | + private Long id; |
| 119 | + |
| 120 | + private String name; |
| 121 | + private Integer born; |
| 122 | + |
| 123 | + // other fields... |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +**Relationship Properties:** |
| 128 | +```java |
| 129 | +@RelationshipProperties |
| 130 | +public class ActedIn { |
| 131 | + |
| 132 | + @RelationshipId |
| 133 | + private Long id; |
| 134 | + |
| 135 | + @TargetNode |
| 136 | + private Person actor; |
| 137 | + |
| 138 | + private List<String> roles; |
| 139 | + private Integer year; |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +### @RelationshipId |
| 144 | + |
| 145 | +The `@RelationshipId` annotation marks a field as the relationship's internal ID. |
| 146 | + |
| 147 | +**Location:** `org.springframework.data.falkordb.core.schema.RelationshipId` |
| 148 | + |
| 149 | +```java |
| 150 | +@RelationshipProperties |
| 151 | +public class Friendship { |
| 152 | + |
| 153 | + @RelationshipId |
| 154 | + private Long id; // Relationship's internal ID |
| 155 | + |
| 156 | + @TargetNode |
| 157 | + private Person friend; |
| 158 | + |
| 159 | + private LocalDate since; |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +## Repository Examples |
| 164 | + |
| 165 | +### Complete Repository Interface |
| 166 | + |
| 167 | +```java |
| 168 | +public interface MovieRepository extends FalkorDBRepository<Movie, String> { |
| 169 | + |
| 170 | + // Derived query methods |
| 171 | + List<Movie> findByReleasedGreaterThan(Integer year); |
| 172 | + |
| 173 | + // Custom queries with different parameter binding styles |
| 174 | + @Query("MATCH (m:Movie) WHERE m.released > $year RETURN m") |
| 175 | + List<Movie> findMoviesReleasedAfter(@Param("year") Integer year); |
| 176 | + |
| 177 | + @Query("MATCH (m:Movie) WHERE m.title CONTAINS $0 RETURN m") |
| 178 | + List<Movie> findMoviesByTitleContaining(String titlePart); |
| 179 | + |
| 180 | + // Complex relationship queries |
| 181 | + @Query("MATCH (m:Movie {title: $title})-[r:ACTED_IN]-(p:Person) " + |
| 182 | + "RETURN m, collect(r), collect(p)") |
| 183 | + Optional<Movie> findMovieWithActors(@Param("title") String title); |
| 184 | + |
| 185 | + // Entity parameter queries |
| 186 | + @Query("MATCH (m:Movie {title: $movie.__id__})-[:ACTED_IN]-(p:Person) RETURN p") |
| 187 | + List<Person> findActorsInMovie(@Param("movie") Movie movie); |
| 188 | + |
| 189 | + // Count and exists queries |
| 190 | + @Query(value = "MATCH (m:Movie) WHERE m.released > $year RETURN count(m)", count = true) |
| 191 | + Long countMoviesReleasedAfter(@Param("year") Integer year); |
| 192 | + |
| 193 | + @Query(value = "MATCH (m:Movie {title: $title}) RETURN count(m) > 0", exists = true) |
| 194 | + Boolean existsByTitle(@Param("title") String title); |
| 195 | + |
| 196 | + // Write operations |
| 197 | + @Query(value = "MATCH (m:Movie {title: $title}) SET m.updated = timestamp() RETURN m", |
| 198 | + write = true) |
| 199 | + Movie updateMovieTimestamp(@Param("title") String title); |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +## Best Practices |
| 204 | + |
| 205 | +### Query Annotation Best Practices |
| 206 | + |
| 207 | +1. **Use meaningful parameter names** with `@Param` for better readability |
| 208 | +2. **Mark write operations** with `write = true` for proper transaction handling |
| 209 | +3. **Use count/exists flags** for performance optimization on aggregate queries |
| 210 | +4. **Avoid N+1 queries** by using `collect()` in Cypher for related data |
| 211 | + |
| 212 | +### Relationship Properties Best Practices |
| 213 | + |
| 214 | +1. **Always use @RelationshipId** for the relationship's internal ID field |
| 215 | +2. **Use @TargetNode** to clearly identify the target node field |
| 216 | +3. **Keep relationship properties simple** and avoid deep nesting |
| 217 | +4. **Consider performance implications** of relationship properties in queries |
| 218 | + |
| 219 | +## Migration from Spring Data Neo4j |
| 220 | + |
| 221 | +These annotations are designed to be compatible with Spring Data Neo4j patterns: |
| 222 | + |
| 223 | +- `@Query` works similarly to Neo4j's `@Query` annotation |
| 224 | +- `@TargetNode` replaces Neo4j's `@TargetNode` with the same semantics |
| 225 | +- `@RelationshipId` provides the same functionality as Neo4j's `@RelationshipId` |
| 226 | + |
| 227 | +The main differences are: |
| 228 | +- Package names use `falkordb` instead of `neo4j` |
| 229 | +- FalkorDB-specific optimizations and features |
| 230 | +- Integration with FalkorDB's graph query language |
0 commit comments