1. What is JDBC?
    • JDBC (Java Database Connectivity) is a Java API that manages connecting to a database, executing queries and commands, and handling result sets obtained from the database.
  2. What are the core components of JDBC?
    • The core components of JDBC include DriverManager, Connection, Statement, ResultSet, and SQLException.
  3. How do you connect to a database using JDBC?
    • Use DriverManager to obtain a Connection object by providing a JDBC URL, username, and password specific to the database.
  4. What is an ORM?
    • ORM (Object-Relational Mapping) is a programming technique for converting data between incompatible type systems using object-oriented programming languages. It creates a “virtual object database” that can be used from within a programming language.
  5. Can you name some popular ORM frameworks for Java?
    • Some popular ORM frameworks for Java include Hibernate, JPA (Java Persistence API), EclipseLink, and MyBatis.
  6. What is the difference between JDBC and ORM frameworks?
    • JDBC is a lower-level API for executing SQL queries directly, while ORM frameworks abstract the database access by mapping database tables to application objects.
  7. What is a JDBC Driver?
    • A JDBC Driver is a software component that enables Java applications to interact with a database. Different types of drivers are available for various databases.
  8. What are the types of JDBC drivers?
    • The types include Type 1 (JDBC-ODBC bridge), Type 2 (native-API driver), Type 3 (network protocol driver), and Type 4 (thin driver).
  9. How do you perform a query using JDBC?
    • Create a Statement or PreparedStatement object using a Connection, execute a SQL query with it, and process the ResultSet.
  10. What is a PreparedStatement in JDBC?
    • A PreparedStatement is a precompiled SQL statement that can be executed multiple times with different parameters, providing improved performance and security against SQL injection.
  11. What is Hibernate?
    • Hibernate is a powerful, high-performance ORM framework that simplifies the development of Java application to interact with the database. It implements the JPA specifications.
  12. What is JPA?
    • JPA (Java Persistence API) is a Java specification for accessing, persisting, and managing data between Java objects/classes and a relational database.
  13. What is a Session in Hibernate?
    • A Session is a single-threaded, short-lived object representing a conversation between the application and the persistent store. It wraps a JDBC connection.
  14. What is the Entity Manager in JPA?
    • The Entity Manager API is used to create, read, and delete operations for instances of mapped entity classes.
  15. What is the difference between get() and load() methods in Hibernate?
    • The get() method retrieves an object that exists in the database; if it doesn’t exist, it returns null. The load() method creates a proxy and retrieves the object lazily, throwing an exception if the object doesn’t exist.
  16. Explain the first-level cache in Hibernate.
    • The first-level cache is associated with the Session object. Hibernate uses this cache by default to reduce the number of database queries within a session.
  17. What is HQL?
    • HQL (Hibernate Query Language) is an object-oriented query language, similar to SQL, but instead operates on Hibernate’s mapping of objects to the database.
  18. What are transactions in the context of databases?
    • A transaction is a sequence of operations performed as a single logical unit of work. A transaction must be atomic, consistent, isolated, and durable (ACID).
  19. How do you manage transactions in JDBC?
    • Transactions in JDBC are managed by using the Connection object’s setAutoCommit(false) method and explicitly committing or rolling back transactions with commit() or rollback().
  20. What is optimistic locking in ORM?
    • Optimistic locking allows multiple transactions to access the same record without locks. Each transaction checks that no other transaction has altered the data before committing, typically using a version field in the database record.
  21. What is lazy loading in Hibernate?
    • Lazy loading is a design pattern that defers the initialization of an object until it is needed, to improve performance and reduce the system’s memory requirements.
  22. What is the N+1 selects problem in ORM?
    • The N+1 selects problem occurs when an ORM executes one query to retrieve the parent objects and then N additional queries to fetch the related child objects, leading to poor performance.
  23. How can you avoid the N+1 selects problem?
    • You can avoid the N+1 selects problem by using fetch strategies like join fetch, subselect, or batch size in ORM frameworks.
  24. What is the criteria API in Hibernate?
    • The Criteria API is a programmatic way to build queries for fetching entities. It is used for creating dynamic queries directly in the Java code.
  25. What is JTA?
    • JTA (Java Transaction API) allows applications to perform distributed transactions, i.e., transactions that span multiple resources like databases and message queues.
  26. How do you handle exceptions in JDBC?
    • Exceptions in JDBC are handled by catching SQLException or SQLWarning thrown by JDBC methods.
  27. What is a named query in JPA?
    • A named query is a statically defined query with a given name. It is defined in the entity class or in the XML descriptor, allowing for a central location for the management of query statements.
  28. What is the difference between merge() and update() in Hibernate?
    • The update() method is used to update an object in the session if it is in the detached state and reattach it. The merge() method creates a new instance of the entity, copies the changes to it, and returns it.
  29. What is a detached entity in Hibernate?
    • A detached entity is an object that has been persisted, but its Session has been closed. It is no longer associated with an active Hibernate session.
  30. How do you ensure that JDBC resources are properly released?
    • Ensure JDBC resources like ResultSet, Statement, and Connection are closed in a finally block or use try-with-resources statement to automatically close them after use.