<commit_before>[[reference_programming-model_repositories]]
= CRUD with repositories

The repositories provided by Spring Data Neo4j build on the composable repository infrastructure in http://static.springsource.org/spring-data/data-commons/docs/current/reference/html/#repositories[Spring Data Commons]. 
These allow for interface-based composition of repositories consisting of provided default implementations for certain interfaces and additional custom implementations for other methods.

Spring Data Neo4j comes with a single `org.springframework.data.repository.PagingAndSortingRepository` specialisation called
`GraphRepository<T>` used for all object-graph mapping repositories.  
This sub-interface also adds specific finder methods that take a _depth_ argument to control the horizon with which related entities are fetched and saved.
Generally, it provides all the desired repository methods. 
If other operations are required then the additional repository interfaces should be added to the individual interface declaration.

[NOTE]
====
`GraphRepository` no longer combines `IndexRepository` and `TraversalRepository` because, for reasons explained
earlier, these features are no longer supported in Spring Data Neo4j as of version 4.
====


== GraphRepository

As of SDN 4, this `GraphRepository<T>` should be the interface from which your entity repository interfaces inherit, with `T` being specified as the domain entity type to persist.

Examples of methods you get for free out of `GraphRepository` are as follows.
For all of these examples the ID parameter is a `Long` that matches the graph ID:

Load an entity instance via an id::
`T findOne(id)`

Check for existence of an id in the graph::
`boolean exists(id)`

Iterate over all nodes of a node entity type::
`Iterable<T> findAll()`
`Iterable<T> findAll(Sort ...)`
`Page<T> findAll(Pageable ...)`

Count the instances of the repository entity type::
`Long count()`

Save entities::
`T save(T)` and `Iterable<T> save(Iterable<T>)`

Delete graph entities::
`void delete(T)`, `void delete(Iterable<T>)`, and `void deleteAll()`

== Query and Finder Methods

[[reference_programming_model_annotatedQueries]]
=== Annotated queries

Queries using the Cypher graph query language can be supplied with the `@Query` annotation. 

That means a repository method annotated with +
`@Query("MATCH (:Actor {name:{name}})-[:ACTED_IN]->(m:Movie) return m")` +
will use the supplied query query to retrieve data from Neo4j. 

The named or indexed parameter `{param}` will be substituted by the actual method parameter. 
Node and Relationship-Entities are handled directly and converted into their respective ids, Iterables thereof as well. 
All other parameters types are provided directly (i.e. Strings, Longs, etc).

[NOTE]
====
In the current version, custom queries do not support paging, sorting or a custom depth.
====

=== Query results

Typical results for queries are `Iterable<Type>`, `Iterable<Map<String,Object>>` or simply `Type`.<commit_msg>DATAGRAPH-765 - Documentation updates for custom query entity mapping and paths.<commit_after>[[reference_programming-model_repositories]]
= CRUD with repositories

The repositories provided by Spring Data Neo4j build on the composable repository infrastructure in http://static.springsource.org/spring-data/data-commons/docs/current/reference/html/#repositories[Spring Data Commons]. 
These allow for interface-based composition of repositories consisting of provided default implementations for certain interfaces and additional custom implementations for other methods.

Spring Data Neo4j comes with a single `org.springframework.data.repository.PagingAndSortingRepository` specialisation called
`GraphRepository<T>` used for all object-graph mapping repositories.  
This sub-interface also adds specific finder methods that take a _depth_ argument to control the horizon with which related entities are fetched and saved.
Generally, it provides all the desired repository methods. 
If other operations are required then the additional repository interfaces should be added to the individual interface declaration.

[NOTE]
====
`GraphRepository` no longer combines `IndexRepository` and `TraversalRepository` because, for reasons explained
earlier, these features are no longer supported in Spring Data Neo4j as of version 4.
====


== GraphRepository

As of SDN 4, this `GraphRepository<T>` should be the interface from which your entity repository interfaces inherit, with `T` being specified as the domain entity type to persist.

Examples of methods you get for free out of `GraphRepository` are as follows.
For all of these examples the ID parameter is a `Long` that matches the graph ID:

Load an entity instance via an id::
`T findOne(id)`

Check for existence of an id in the graph::
`boolean exists(id)`

Iterate over all nodes of a node entity type::
`Iterable<T> findAll()`
`Iterable<T> findAll(Sort ...)`
`Page<T> findAll(Pageable ...)`

Count the instances of the repository entity type::
`Long count()`

Save entities::
`T save(T)` and `Iterable<T> save(Iterable<T>)`

Delete graph entities::
`void delete(T)`, `void delete(Iterable<T>)`, and `void deleteAll()`

== Query and Finder Methods

[[reference_programming_model_annotatedQueries]]
=== Annotated queries

Queries using the Cypher graph query language can be supplied with the `@Query` annotation. 

That means a repository method annotated with +
`@Query("MATCH (:Actor {name:{name}})-[:ACTED_IN]->(m:Movie) return m")` +
will use the supplied query query to retrieve data from Neo4j. 

The named or indexed parameter `{param}` will be substituted by the actual method parameter. 
Node and Relationship-Entities are handled directly and converted into their respective ids, Iterables thereof as well. 
All other parameters types are provided directly (i.e. Strings, Longs, etc).

[NOTE]
====
In the current version, custom queries do not support paging, sorting or a custom depth.
`@Query` does not support mapping a path to domain entities, as such, a path should not be returned from a Cypher query.
Instead, return nodes and relationships to have them mapped to domain entities.
====

=== Query results

Typical results for queries are `Iterable<Type>`, `Iterable<Map<String,Object>>` or simply `Type`.
{'fraction_non_alphanumeric': 0.07928185573562123, 'fraction_numerical': 0.0023832221163012394, 'mean_word_length': 5.503099173553719, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5216600', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1561, 'n_words': 782}