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

  1. 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.
  2. 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).
  3. 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.
  4. 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.
  5. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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 the persistence.xml file.

Advanced

  1. 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.
  2. Q: How can you perform a bulk update with JPQL?
    A: Bulk updates can be performed using the executeUpdate() method on a Query object created from a JPQL statement that specifies the update operation.
  3. 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.
  4. 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.
  5. 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

  1. 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).
  2. 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

  1. Q: Can you use native SQL queries in JPA?
    A: Yes, JPA allows the execution of native SQL queries using the EntityManager.createNativeQuery() method.
  2. Q: How do you map an enum in JPA?
    A: Enums can be mapped using the @Enumerated annotation, specifying either EnumType.ORDINAL or EnumType.STRING as the mapping strategy.
  3. 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.
  4. Q: How do you handle lazy loading in JPA?
    A: Lazy loading in JPA is specified using the fetch attribute of relationship annotations (@OneToOne, @OneToMany, etc.), setting it to FetchType.LAZY.

More Advanced

  1. 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.
  2. 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.
  3. 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 with EntityManager.createNamedQuery().
  4. 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.
  5. 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.
  6. Q: How does JPA handle transactions?
    A: JPA handles transactions through the EntityTransaction interface provided by the EntityManager. Transactions are started, committed, or rolled back using this interface.
  7. 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.
  8. 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.
  9. 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 the AttributeConverter interface, allowing custom logic for database column value conversions.