0% found this document useful (0 votes)
6 views4 pages

App9: Spring Data JPA - Setup and Basics Q1: What Is Spring Data JPA? A

JAP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

App9: Spring Data JPA - Setup and Basics Q1: What Is Spring Data JPA? A

JAP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

App9: Spring Data JPA - Setup and Basics

Q1: What is Spring Data JPA?

• A: Spring Data JPA is a part of the larger Spring Data family. It simplifies the data access layer
by providing repository support for JPA, allowing developers to focus on business logic.

Q2: How do you set up Spring Data JPA in a Spring Boot application?

• A: Steps:

1. Add the Spring Data JPA dependency in pom.xml:

2. <dependency>

3. <groupId>org.springframework.boot</groupId>

4. <artifactId>spring-boot-starter-data-jpa</artifactId>

5. </dependency>

6. <dependency>

7. <groupId>com.h2database</groupId>

8. <artifactId>h2</artifactId>

9. </dependency>

10. Configure the database connection in application.properties or application.yml:

11. spring.datasource.url=jdbc:h2:mem:testdb

12. spring.datasource.driver-class-name=org.h2.Driver

13. spring.datasource.username=sa

14. spring.datasource.password=password

15. spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

16. Create an entity class annotated with @Entity.

17. Create a repository interface extending JpaRepository.

Q3: What are the key annotations in Spring Data JPA?

• A:

o @Entity: Marks a class as a JPA entity.

o @Table: Specifies the table name and other table properties.

o @Id: Marks the primary key.

o @GeneratedValue: Specifies the generation strategy for primary keys.

o @Column: Defines column properties.

Q4: What is the role of JpaRepository?


• A: JpaRepository provides CRUD methods out of the box and extends
PagingAndSortingRepository for pagination and sorting support.

Q5: How do you enable JPA repositories?

• A: Add the @EnableJpaRepositories annotation in the configuration class or rely on Spring


Boot's auto-configuration.

App10: Spring Data JPA - CRUD Operations

Q1: What methods are available in JpaRepository for CRUD operations?

• A:

o save(S entity): Save or update an entity.

o findById(ID id): Retrieve an entity by its ID.

o findAll(): Retrieve all entities.

o deleteById(ID id): Delete an entity by its ID.

o deleteAll(): Delete all entities.

Q2: How do you implement a custom query for fetching data?

• A: Use @Query annotation in the repository:

• @Query("SELECT u FROM User u WHERE u.email = :email")

• User findByEmail(@Param("email") String email);

Q3: How do you handle pagination and sorting?

• A: Use the Pageable and Sort interfaces:

• Page<User> findAll(Pageable pageable);

• List<User> findAll(Sort sort);

Q4: What is the difference between save() and saveAndFlush()?

• A:

o save(): Saves an entity without flushing changes to the database.

o saveAndFlush(): Saves an entity and flushes changes immediately.

App11: Spring Data JPA - Custom Operations

Q1: How do you define a custom repository method?

• A: Extend JpaRepository and define methods based on naming conventions or use @Query
for custom SQL queries.

Q2: How do you enforce constraints like not null, unique, composite keys, etc.?
• A:

o Not Null: Use @Column(nullable = false).

o Unique: Use @Column(unique = true).

o Composite Unique: Use @Table(uniqueConstraints =


@UniqueConstraint(columnNames = {"column1", "column2"})).

o Composite Primary: Use @Embeddable and @EmbeddedId.

Q3: How do you implement a composite primary key?

• A:

1. Create an @Embeddable class with fields for the composite key.

2. Use @EmbeddedId in the entity class.

• @Embeddable

• public class CompositeKey implements Serializable {

• private Long idPart1;

• private Long idPart2;

• // Getters and setters

• }

• @Entity

• public class ExampleEntity {

• @EmbeddedId

• private CompositeKey id;

• }

App12: Spring Data JPA - VAO and DTO

Q1: What is the difference between VAO and DTO?

• A:

o VAO (Value Added Object): Represents an enriched data object containing derived
or computed data.

o DTO (Data Transfer Object): A plain object used to transfer data between layers,
reducing coupling.

Q2: How do you create a DTO in Spring Data JPA?

• A:
1. Define a DTO class with necessary fields.

2. Use a constructor expression or projection in queries:

3. @Query("SELECT new com.example.dto.UserDTO(u.name, u.email) FROM User u")

4. List<UserDTO> findAllUsers();

Q3: What is the purpose of mapping DTOs?

• A: To avoid exposing entity objects directly and ensure data consistency.

Q4: How do you map entity to DTO using ModelMapper or MapStruct?

• A: Use tools like:

o ModelMapper:

o ModelMapper modelMapper = new ModelMapper();

o UserDTO userDTO = modelMapper.map(user, UserDTO.class);

o MapStruct: Annotate with @Mapper and define mapping methods.

Q5: How do you handle nested mappings in DTOs?

• A: Use nested projections or custom mappings in tools like MapStruct.

You might also like