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:
-
Repositories — interfaces that extend Spring Data's standard interfaces, such as
CrudRepository,JpaRepository. They automatically implement basic operations (CRUD). -
Query Methods — creating methods in repositories with names that Spring Data parses and generates corresponding queries.
-
@Query Annotation — allows writing custom JPQL or SQL queries directly in repository methods.
-
Using EntityManager — for more complex operations, you can use
EntityManagerdirectly. -
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.