Spring Data JPA — Spring Technical Interview Questions & Answers
Repository-based persistence without boilerplate DAO code.
Spring Data JPA
- Spring Data JPA lets you talk to the DB through a Java interface.
- Extend JpaRepository<Entity, IdType> to get save/find/delete for free.
- Method names like findByEmail become queries automatically.
- Hibernate is commonly the JPA provider underneath Boot.
- You map tables to @Entity classes.
- Putting complex business rules inside repository interfaces.
- Repository interface + derived query methods = less DAO code.
Technical Interview Tip: Show a tiny repository with findByEmail.
Code Examples for Spring Data JPA
Spring Data JPA — working example
import java.util.List;
import jakarta.persistence.*;
import org.springframework.data.jpa.repository.JpaRepository;
@Entity
@Table(name = "users")
class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String email;
private String name;
// getters/setters omitted
}
interface UserRepository extends JpaRepository<User, Long> {
User findByEmail(String email);
List<User> findByNameContainingIgnoreCase(String q);
}Spring Data JPA — @Entity maps a table; GenerationType.IDENTITY uses DB auto-increment; JpaRepository adds CRUD + derived queries.
How to Explain Spring Data JPA to an Interviewer
What is Data JPA (sb db)?
Used for Student entity H2 / MySQL Not just the heading
Give one small example of Data JPA (sb db).
@Entity + JpaRepository Repository-based persistence without boilerplate DAO code
What mistake do beginners make with Data JPA (sb db)?
LazyInitializationException after the session closes Show the fix
Where do you use Data JPA (sb db)?
H2 / MySQL Student entity