Here are 50 commonly asked interview questions for senior developers and architects in the context of Java Persistence API (JPA), along with their answers:
1. What is JPA?
JPA stands for Java Persistence API, which is a specification for managing relational data in Java applications.
2. What are the key components of JPA?
The key components of JPA are entities, entity managers, the persistence unit, and the object-relational mapping (ORM) metadata.
3. What is an entity in JPA?
An entity is a lightweight Java class that represents a persistent object and is mapped to a database table.
4. What is the role of an entity manager?
The entity manager is responsible for managing the lifecycle of entities, including persisting, updating, and deleting them.
5. What is a persistence unit?
A persistence unit is a logical grouping of related entities and configuration information, defined in the persistence.xml file.
6. How does JPA handle object-relational mapping (ORM)?
JPA uses annotations or XML mappings to define the mapping between entities and database tables.
7. What is the difference between JPA and Hibernate?
Hibernate is an implementation of the JPA specification. JPA provides a standard API, while Hibernate is one of the many frameworks that implement this API.
8. What is the @Entity annotation used for?
The @Entity annotation is used to mark a Java class as an entity.
9. Explain the @Id annotation.
The @Id annotation is used to designate a field as the primary key of an entity.
10. What is the purpose of the @GeneratedValue annotation?
The @GeneratedValue annotation is used to define how a primary key value is generated, such as using an identity column or a sequence.
11. What is the difference between FetchType.LAZY and FetchType.EAGER?
FetchType.LAZY loads associated entities lazily, on-demand, while FetchType.EAGER loads associated entities immediately when the owning entity is loaded.
12. What is the CascadeType.ALL in JPA?
CascadeType.ALL is used to specify that all operations (persist, remove, refresh, merge) should be cascaded to the associated entities.
13. Explain the @JoinColumn annotation.
The @JoinColumn annotation is used to specify the column used for joining an entity association.
14. What is a named query in JPA?
A named query is a pre-defined query that is declared in the entity class or the XML mapping file, and can be used to execute a query by name.
15. What is a native query in JPA?
A native query is a SQL query that can be executed directly using JPA. It is not portable across different databases.
16. What is a composite primary key in JPA?
A composite primary key is a primary key composed of multiple fields in an entity.
17. How do you handle relationships between entities in JPA?
Relationships between entities are handled using annotations like @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany.
18. What is the purpose of the @Version annotation?
The @Version annotation is used for optimistic locking, where a version field is incremented on each update to prevent concurrent modifications.
19. What is the purpose of the EntityManagerFactory in JPA?
The EntityManagerFactory is responsible for creating and managing EntityManager instances.
20. What is the difference between merge() and update() methods in JPA?
The merge() method is used to merge the state of a detached entity into a persistence context, while the update() method is used to update the state of a persistent entity.
21. How can you retrieve a subset of results using JPA?
JPA provides methods like setFirstResult() and setMaxResults() to retrieve a subset of query results.
22. What is the purpose of the @Transient annotation?
The @Transient annotation is used to mark a field that should not be persisted to the database.
23. How do you define a many-to-many relationship in JPA?
A many-to-many relationship is defined using the @ManyToMany annotation on both sides of the relationship.
24. What is the purpose of the @OrderBy annotation?
The @OrderBy annotation is used to specify the order in which a collection-valued association should be retrieved from the database.
25. What is the purpose of the @MappedSuperclass annotation?
The @MappedSuperclass annotation is used to define a superclass that provides common mappings to its subclasses.
26. How do you handle inheritance in JPA?
Inheritance in JPA can be handled using the @Inheritance annotation and specifying the inheritance strategy, such as JOINED, SINGLE_TABLE, or TABLE_PER_CLASS.
27. What is the difference between EntityManager.find() and EntityManager.getReference()?
EntityManager.find() returns a managed entity instance by its primary key, while EntityManager.getReference() returns a lightweight reference to the entity without hitting the database until necessary.
28. What is a callback method in JPA?
Callback methods are methods defined in an entity class that are invoked at certain points in the lifecycle of the entity, such as @PrePersist or @PostLoad.
29. Explain the FetchType.LAZY and FetchType.EAGER in relation to collection mappings.
FetchType.LAZY for a collection mapping means the associated collection is loaded lazily when accessed, while FetchType.EAGER means it is loaded immediately when the owning entity is loaded.
30. What is a secondary table in JPA?
A secondary table is an additional table associated with an entity that contains extra fields not present in the primary table.
31. How do you handle optimistic locking in JPA?
Optimistic locking is handled using a version field annotated with @Version. The value of this field is automatically incremented on each update, and concurrent modifications are detected based on it.
32. What is the purpose of the @JoinColumn annotation in a one-to-many relationship?
The @JoinColumn annotation is used to specify the column used for joining the owning side of a one-to-many relationship.
33. How do you handle a one-to-one relationship with shared primary key in JPA?
A one-to-one relationship with shared primary key is handled by using the @PrimaryKeyJoinColumn annotation on the owning side of the relationship.
34. What is the purpose of the EntityManager interface?
The EntityManager interface is the primary interface through which JPA operations are performed. It provides methods for CRUD operations and querying.
35. What is the purpose of the EntityManager.merge() method?
The merge() method is used to merge the state of a detached entity into a persistence context. If the entity does not exist, it will be persisted.
36. How do you perform pagination in JPA?
Pagination in JPA can be achieved by using the setFirstResult() and setMaxResults() methods on the Query or TypedQuery object.
37. What is the purpose of the @SequenceGenerator annotation?
The @SequenceGenerator annotation is used to define a named sequence generator for generating primary key values.
38. How do you use JPA in a Java EE application?
In a Java EE application, you can inject the EntityManager using the @PersistenceContext annotation and perform database operations using JPA.
39. What is a compound index in JPA?
A compound index is an index that includes multiple columns. It can be defined using the @Index annotation on the entity class.
40. What is the purpose of the EntityManager.persist() method?
The persist() method is used to make a new entity instance persistent. It ensures that the entity is associated with a persistence context and will be inserted into the database upon transaction commit.
41. How do you handle transactions in JPA?
Transactions in JPA can be managed programmatically using the EntityManager.getTransaction() method and the begin(), commit(), and rollback() methods.
42. What is the purpose of the @Temporal annotation?
The @Temporal annotation is used to specify the type of a temporal database field, such as DATE, TIME, or TIMESTAMP.
43. How do you handle a self-referencing relationship in JPA?
A self-referencing relationship in JPA is handled by mapping the association field with the @ManyToOne or @OneToMany annotation, depending on the relationship type.
44. What is a named native query in JPA?
A named native query is a pre-defined SQL query that is declared in the entity class or the XML mapping file and can be executed by name using the createNativeQuery() method.
45. What is the purpose of the @Column annotation?
The @Column annotation is used to specify the mapping of an entity field to a database column, including its name, type, and constraints.
46. How do you handle a many-to-many relationship with additional columns in the join table?
A many-to-many relationship with additional columns in the join table is handled by using an intermediate entity to represent the join table, with additional fields for the extra columns.
47. What is the purpose of the @Access annotation?
The @Access annotation is used to specify the access type (field or property) for an entity class or a mapped superclass.
48. How do you perform bulk updates or deletes in JPA?
Bulk updates or deletes can be performed using JPQL (Java Persistence Query Language) or native SQL queries executed through the EntityManager.
49. What is the purpose of the @SecondaryTable annotation?
The @SecondaryTable annotation is used to specify a secondary table associated with an entity.
50. How do you use JPA in a standalone Java application?
In a standalone Java application, you can create an EntityManagerFactory using the Persistence.createEntityManagerFactory() method and manage transactions programmatically using the EntityManager.