Sobes.tech
Junior — Middle

What methods of interacting with a database through Spring Data do you know?

sobes.tech AI

Answer from AI

Spring Data provides several ways to interact with the database:

  1. Repositories — interfaces that extend Spring Data's standard interfaces, such as CrudRepository, JpaRepository. They automatically implement basic operations (CRUD).

  2. Query Methods — creating methods in repositories with names that Spring Data parses and generates corresponding queries.

  3. @Query Annotation — allows writing custom JPQL or SQL queries directly in repository methods.

  4. Using EntityManager — for more complex operations, you can use EntityManager directly.

  5. Projections and DTOs — for selecting only the necessary fields.

Example of a query method by name:

public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByLastName(String lastName);
}

This method will automatically execute a query that selects users with the specified last name.