JPA (Java Persistence API) can help you solidify your understanding of this vital Java specification for managing relational data in Java applications. Here’s a list that covers a range of basic to more advanced topics:
Basics
- Q: What is JPA?
A: JPA stands for Java Persistence API. It’s a Java specification for accessing, persisting, and managing data between Java objects/classes and a relational database. - Q: What are the main components of JPA?
A: The main components include the EntityManager, the EntityManagerFactory, the Persistence Unit, Entities, and JPQL (Java Persistence Query Language). - Q: What is an Entity in JPA?
A: An Entity represents a table in a database. Each entity instance corresponds to a row in the table. - Q: How do you define an Entity in JPA?
A: An Entity is defined by annotating a class with@Entity
. The class must have a no-argument constructor and a primary key. - Q: What is the role of the EntityManager interface in JPA?
A: The EntityManager interface is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.
Intermediate
- Q: What is JPQL, and how does it differ from SQL?
A: JPQL is Java Persistence Query Language, designed for querying entities in the database. Unlike SQL, which operates on tables and columns, JPQL operates on entities, attributes, and relationships. - Q: How do you specify a primary key in JPA?
A: A primary key is specified using the@Id
annotation on the field that serves as the primary key. - Q: What is the EntityManagerFactory?
A: The EntityManagerFactory is an interface used to create EntityManager instances. It’s created at application startup and typically exists for the duration of the application. - Q: How do you handle relationships in JPA?
A: Relationships in JPA are handled through annotations such as@OneToOne
,@OneToMany
,@ManyToOne
, and@ManyToMany
, depending on the relationship type. - Q: What is a Persistence Unit in JPA?
A: A Persistence Unit defines a set of all entity classes that are managed by EntityManager in an application. It is specified in thepersistence.xml
file.
Advanced
- Q: What is the difference between
@Entity
and@Table
in JPA?
A:@Entity
marks a class as a JPA entity, whereas@Table
is used to specify the table in the database with which the entity is mapped. - Q: How can you perform a bulk update with JPQL?
A: Bulk updates can be performed using theexecuteUpdate()
method on a Query object created from a JPQL statement that specifies the update operation. - Q: What are Entity Listeners in JPA?
A: Entity Listeners are used to listen to events in an entity’s lifecycle, such as pre-persist, post-persist, pre-remove, post-remove, pre-update, and post-load. - Q: What is the N+1 select problem in JPA, and how can you prevent it?
A: The N+1 select problem occurs when an operation to fetch N entities results in N+1 queries to the database. It can be prevented by using fetch joins in JPQL or specifying fetch strategies in entity mapping. - Q: What is the
@Version
annotation used for in JPA?
A: The@Version
annotation is used for optimistic locking. It helps to handle concurrent modifications of the same entity by different transactions.
Performance
- Q: How does JPA handle caching?
A: JPA supports two levels of caching: the first level cache (associated with the EntityManager) and the second level cache (shared across EntityManagers). - Q: What are the benefits of using a second-level cache in JPA?
A: The second-level cache can significantly improve application performance by reducing the number of database accesses, especially for frequently read but rarely modified entities.
Miscellaneous
- Q: Can you use native SQL queries in JPA?
A: Yes, JPA allows the execution of native SQL queries using theEntityManager.createNativeQuery()
method. - Q: How do you map an enum in JPA?
A: Enums can be mapped using the@Enumerated
annotation, specifying eitherEnumType.ORDINAL
orEnumType.STRING
as the mapping strategy. - Q: What is a composite primary key, and how is it implemented in JPA?
A: A composite primary key is a primary key consisting of multiple fields. In JPA, it’s implemented by creating a separate class annotated with@Embeddable
or@IdClass
and using it in the entity class. - Q: How do you handle lazy loading in JPA?
A: Lazy loading in JPA is specified using thefetch
attribute of relationship annotations (@OneToOne
,@OneToMany
, etc.), setting it toFetchType.LAZY
.
More Advanced
- Q: What is the difference between merge and persist in JPA?
A:persist
is used to save a new entity to the database, making it managed and persistent.merge
is used to update an existing entity in the database, returning a managed entity instance. - Q: What is the Criteria API in JPA?
A: The Criteria API is a programmatically defined type-safe query API that allows for building SQL-like queries using Java programming constructs instead of strings. - Q: How do you declare and use named queries in JPA?
A: Named queries are declared using the@NamedQuery
or@NamedQueries
annotation on an entity class. They are used by creating a Query instance withEntityManager.createNamedQuery()
. - Q: What strategies exist for inheritance mapping in JPA?
A: JPA supports single table, joined, and table per class inheritance mapping strategies, specified using the@Inheritance
annotation. - Q: How do you configure optimistic locking in JPA?
A: Optimistic locking is configured using the@Version
annotation on a version field in the entity class. - Q: How does JPA handle transactions?
A: JPA handles transactions through theEntityTransaction
interface provided by theEntityManager
. Transactions are started, committed, or rolled back using this interface. - Q: What is a named entity graph in JPA?
A: A named entity graph is a template for a fetch plan, defined on an entity to specify which attributes or entity relationships should be eagerly fetched. - Q: How do you update an entity in JPA?
A: An entity can be updated by retrieving it through an EntityManager, modifying its attributes, and committing the transaction. - Q: What are conversion strategies in JPA for non-standard types?
A: For non-standard types, JPA uses the@Convert
annotation to specify a custom converter class that implements theAttributeConverter
interface, allowing custom logic for database column value conversions.