Register Login

JPA Interview Questions and Answers

Updated May 23, 2025

What is JPA and what does JPA stand for?

JPA stands for Java Persistence API. It is a specification for object-relational mapping (ORM) in Java. JPA provides a standard for managing relational data in Java applications, allowing developers to map Java objects to database tables and vice versa. It defines a set of tools and interfaces which can be implemented by frameworks such as Hibernate and EclipseLink.

What are the advantages of JPA?

The advantages of JPA include:

  • Improves productivity by reducing boilerplate code with annotations and configurations.
  • Database independence due to its abstraction layer over SQL.
  • Support for JPQL (Java Persistence Query Language), a platform-independent query language.
  • Supports caching, lazy loading, and automatic dirty checking.
  • Enables switching between different providers (e.g., Hibernate, EclipseLink) with minimal changes.

What is a JPA repository?

A JPA repository is a mechanism provided by Spring Data JPA that encapsulates the logic required to access data sources. It reduces boilerplate code and supports CRUD operations, pagination, sorting, and custom queries by extending JpaRepository interface.

What is EntityManager in JPA?

EntityManager is the primary JPA interface used to interact with the persistence context. It provides methods such as persist(), find(), merge(), remove(), and createQuery() to manage entity instances and execute database operations.

How to validate username and password using JPA?

To validate username and password using JPA, typically:

  • Define a User entity with validation annotations like @NotNull and @Size.
  • Use Spring Boot with Hibernate Validator for automatic validation.
  • Fetch user by username and check password manually or with encoded comparison using BCryptPasswordEncoder.

What is the persistence context in JPA?

The persistence context is a set of managed entity instances that exist in memory. It ensures each entity with a given primary key is unique in the context. EntityManager maintains this context and automatically synchronizes changes with the database during transaction commit.

What is named query and how to use named query in JPA?

A named query is a static query defined using annotations like @NamedQuery at the entity level. These queries are compiled when the application starts and can be reused multiple times via EntityManager.

How to add JPA plugin in Eclipse?

  1. Install Eclipse IDE for Java EE Developers.
  2. Go to Help > Eclipse Marketplace and search for "JPA" if not already available.
  3. Create a new Dynamic Web Project.
  4. Right-click project > Properties > Project Facets > Enable JPA and choose version.
  5. Select JPA implementation library (e.g., EclipseLink or Hibernate).

How to write a custom query in JPA?

To write a custom query in Spring Data JPA:

  • Define a method in the repository interface.
  • Annotate it with @Query.
  • Use JPQL or native SQL by setting nativeQuery = true.

What is a typed query in JPA?

TypedQuery is an interface introduced in JPA 2.0 that extends the Query interface. It allows for type-safe queries, enabling compile-time checking of query results. TypedQuery instances are created by EntityManager when the result type is known.

What is pagination in JPA?

Pagination in JPA allows retrieving subsets of data using setFirstResult() and setMaxResults() methods in JPQL. In Spring Data JPA, pagination is implemented using Pageable and Page interfaces.

How to save UUIDs using JPA?

UUIDs can be used as primary keys in JPA entities. Annotate the field with @Id and use @GeneratedValue with strategy GenerationType.AUTO or GenerationType.IDENTITY. You can also use @GenericGenerator from Hibernate with "uuid2" strategy.

What is a JPA provider?

A JPA provider is an implementation of the JPA specification. Examples include Hibernate, EclipseLink, and OpenJPA. The provider handles the low-level operations of interacting with the database.

What is JPA remove?

To delete an entity in JPA, retrieve it using find() or another method, and call remove() within a transaction. Deletion only occurs when the transaction is committed. Alternatives include cascading remove, orphan removal, and JPQL delete queries.

What is the difference between persist() and merge() in JPA?

persist(): Inserts a new entity into the database. It throws an exception if the entity already exists.

merge(): Updates the existing entity if it exists, or inserts a new one if it doesn’t. It returns a managed entity copy.

What is the difference between find() and getReference() in JPA?

find(): Immediately accesses the database and returns a fully initialized entity or null if not found.

getReference(): Returns a proxy object and doesn’t hit the database immediately. The data is fetched lazily when accessed.

What are entity states in JPA?

JPA defines 4 entity states:

  • New (Transient): Not associated with a persistence context.
  • Managed (Persistent): Attached to a persistence context and tracked by EntityManager.
  • Detached: Was once managed but is no longer tracked.
  • Removed: Scheduled for deletion but not yet deleted from the database.

What is lazy and eager fetching in JPA?

Lazy fetching: Related entities are fetched on demand (when accessed).

Eager fetching: Related entities are fetched immediately along with the parent entity.

What is the difference between JPQL and native SQL in JPA?

JPQL (Java Persistence Query Language): Object-oriented and independent of the database.

Native SQL: Database-specific and operates directly on tables and columns.

What is the difference between CascadeType.PERSIST and CascadeType.MERGE?

CascadeType.PERSIST: When the parent is persisted, the associated entities are also persisted.

CascadeType.MERGE: When the parent is merged, the associated entities are also merged.


×