Spring Data JPA MCQ

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?

a) Spring Data JPA aims to reduce the amount of boilerplate code required for common database operations.
b) Spring Data JPA aims to provide the implementation for JPA interfaces
c) Spring Data JPA is a JPA provider and implementation for JPA interfaces
d) Spring Data JPA is an ORM framework that provides an implementation for JPA interfaces

Answer:

a) Spring Data JPA aims to reduce the amount of boilerplate code required for common database operations.

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?

a) spring-boot-starter-data-jpa
b) spring-boot-starter-web
c) spring-boot-starter-test
d) spring-boot-starter-security

Answer:

a) spring-boot-starter-data-jpa

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?

a) EclipseLink
b) MyBatis
c) TopLink
d) Hibernate

Answer:

d) Hibernate

Explanation:

Spring Data JPA uses Hibernate as a default JPA provider.

4. Which annotation marks a class as an entity in JPA?

a) @EntityClass
b) @JPAEntity
c) @Entity
d) @TableEntity

Answer:

c) @Entity

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?

a) Repository
b) JpaRepository
c) JpaRepository
d) SpringRepository

Answer:

b) JpaRepository

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?

a) By annotating a method with @Query
b) By creating a method in the repository with a specific naming convention
c) By using the @DerivedQuery annotation
d) By creating an XML configuration

Answer:

b) By creating a method in the repository with a specific naming convention

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?

a) @InjectRepository
b) @AutoInject
c) @Autowire
d) @Resource

Answer:

c) @Autowire

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?

a) EAGER
b) LAZY
c) IMMEDIATE
d) BOTH

Answer:

c) IMMEDIATE

Explanation:

JPA provides two fetch types: EAGER and LAZY.

9. How do you execute a native query in Spring Data JPA?

a) @Native
b) @SQL
c) @Execute
d) @Query(nativeQuery = true)

Answer:

d) @Query(nativeQuery = true)

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?

a) PERSIST
b) REMOVE
c) MERGE
d) UPDATE

Answer:

d) UPDATE

Explanation:

UPDATE is not a valid CascadeType in JPA.

11. How can you mark a field as the primary key in a JPA entity?

a) @PrimaryKey
b) @EntityKey
c) @Id
d) @Key

Answer:

c) @Id

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?

a) @Column(name=”custom_name”)
b) @FieldName(“custom_name”)
c) @TableColumn(“custom_name”)
d) @DatabaseColumn(“custom_name”)

Answer:

a) @Column(name=”custom_name”)

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?

a) @CustomQuery
b) @JPAQuery
c) @Query
d) @ExecuteQuery

Answer:

c) @Query

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?

a) @ManyToOne
b) @OneToMany
c) @ManyToMany
d) @OneToOne

Answer:

a) @ManyToOne

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?

a) deleteByFieldName
b) removeByFieldName
c) eraseByFieldName
d) exterminateByFieldName

Answer:

a) deleteByFieldName

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?

a) deleteByFieldName
b) findByFieldName
c) queryByFieldName
d) readByFieldName

Answer:

a) deleteByFieldName

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?

a) Using the @Pageable annotation
b) Using the Page and Pageable interfaces
c) Using the @Pagination annotation
d) Using the Paginator class

Answer:

b) Using the Page and Pageable interfaces

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?

a) GenerationType.AUTO
b) GenerationType.SEQUENCE
c) GenerationType.IDENTITY
d) GenerationType.TABLE

Answer:

c) GenerationType.IDENTITY

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?

a) @Embeddable
b) @Embedded
c) @Include
d) @PartOf

Answer:

b) @Embedded

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?

a) By using the @Transactional annotation.
b) By configuring the transaction properties in the application.properties file.
c) By extending the JpaTransactionManager class.
d) By adding the @EnableTransactionManagement annotation to the configuration class

Answer:

a) By using the @Transactional annotation.

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.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top