# Spring Data Repositories

## How

* Using annotated entity, we create an interface:

```java
@Entity
public class Author {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  private String firstName;
  private String lastName;
  @ManyToMany(mappedBy = "authors")
  private Set<Book> books;

  public Author() {
  }

  public Author(String firstName, String lastName, Set<Book> books) {...}

  // Omitted setters/getter/equals/hashcode/toString
}

public interface AuthorRepository extends CrudRepository<Author, Long>{}
```

* This CrudRepository interface provides several CRUD operations, and Spring will provide the implementation for us to use.
* Initialiasing with data
  * Use of `CommandLineRunner` which has a runnable
    * Spring will find any of these impementations and run them
    * Can be used as a main method
  * Can inject repository created, and use crud methods to add data or get data etc


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hanfak.gitbook.io/workspace/languages/java/framework/spring/spring-data-repositories.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
