- 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.
- What are the core components of JDBC?
- The core components of JDBC include DriverManager, Connection, Statement, ResultSet, and SQLException.
- 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.
- 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.
- Can you name some popular ORM frameworks for Java?
- Some popular ORM frameworks for Java include Hibernate, JPA (Java Persistence API), EclipseLink, and MyBatis.
- 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.
- 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.
- 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).
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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. Theload()
method creates a proxy and retrieves the object lazily, throwing an exception if the object doesn’t exist.
- The
- 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.
- 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.
- 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).
- 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 withcommit()
orrollback()
.
- Transactions in JDBC are managed by using the Connection object’s
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- How do you handle exceptions in JDBC?
- Exceptions in JDBC are handled by catching
SQLException
orSQLWarning
thrown by JDBC methods.
- Exceptions in JDBC are handled by catching
- 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.
- 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. Themerge()
method creates a new instance of the entity, copies the changes to it, and returns it.
- The
- 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.
- 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.