Spring Data JPA is an integral part of the Spring ecosystem that provides easy data access from Spring applications to relational databases. Let’s put your knowledge to the test with this beginner-level quiz!
Note that each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. What is the aim of Spring Data JPA?
Answer:
Explanation:
Spring Data JPA aims to reduce the amount of boilerplate code required for common database operations and make it easier to work with relational databases.
2. Which dependency is required to use Spring Data JPA in a Spring Boot application?
Answer:
Explanation:
To use Spring Data JPA in a Spring Boot application, you need to include the spring-boot-starter-data-jpa dependency in your project’s dependencies. This starter dependency includes the necessary dependencies for working with JPA and provides auto-configuration for Spring Data JPA.
3. Which is the default JPA provider the Spring Data JPA internally uses?
Answer:
Explanation:
Spring Data JPA uses Hibernate as a default JPA provider.
4. Which annotation marks a class as an entity in JPA?
Answer:
Explanation:
The @Entity annotation is used to indicate that a class is a JPA entity.
5. Which interface do we extend to create a repository in Spring Data JPA?
Answer:
Explanation:
JpaRepository is the interface provided by Spring Data JPA for CRUD operations.
6. How can you define a derived query in Spring Data JPA?
Answer:
Explanation:
Spring Data JPA allows developers to create queries simply by defining a method with a naming convention without writing the actual query.
7. Which annotation is used to autowire a repository into a Spring component?
Answer:
Explanation:
The @Autowire annotation is used to inject Spring beans, including repositories, into other components.
8. Which of the following is NOT a fetch type in JPA?
Answer:
Explanation:
JPA provides two fetch types: EAGER and LAZY.
9. How do you execute a native query in Spring Data JPA?
Answer:
Explanation:
For native queries in Spring Data JPA, you use the @Query annotation and set the nativeQuery attribute to true.
10. Which of the following is NOT a valid Cascade type in JPA?
Answer:
Explanation:
UPDATE is not a valid CascadeType in JPA.
11. How can you mark a field as the primary key in a JPA entity?
Answer:
Explanation:
The @Id annotation is used to denote a field as the primary key in a JPA entity.
12. How can you customize the column name for a field in a JPA entity?
Answer:
Explanation:
The @Column annotation allows you to specify the details of the column to which an entity field is mapped, including customizing the column name.
13. Which annotation is used to specify a custom query in Spring Data JPA?
Answer:
Explanation:
The @Query annotation is used to define custom queries in Spring Data JPA.
14. How can you indicate a Many-To-One relationship in JPA?
Answer:
Explanation:
The @ManyToOne annotation indicates a many-to-one relationship between two entities.
15. In a Spring Data JPA repository, how can you indicate a method should delete by a specific field?
Answer:
Explanation:
The convention in Spring Data JPA is deleteBy[FieldName] to indicate deletion by a specific field.
16. Which of the following denotes a derived delete query in Spring Data JPA?
Answer:
Explanation:
The naming convention deleteBy[FieldName] denotes a derived delete query in Spring Data JPA.
17. How can you perform pagination in Spring Data JPA?
Answer:
Explanation:
In Spring Data JPA, pagination is achieved using the Page and Pageable interfaces.
18. In JPA, which strategy generates a primary key assigned by the database?
Answer:
Explanation:
The GenerationType.IDENTITY strategy indicates that the database should assign the primary key value.
19. Which annotation in JPA allows you to embed another object as part of your entity?
Answer:
Explanation:
The @Embedded annotation is used to embed another object (annotated with @Embeddable) as part of your entity.
20. How can you control transaction management in Spring Data JPA?
Answer:
Explanation:
Transaction management in Spring Data JPA can be controlled by using the @Transactional annotation. By applying this annotation to service methods or repository methods, you can define transactional boundaries and control the behavior of database operations.