- What is Hibernate?
Ans. Hibernate ORM (Hibernate in short) is an open-source, lightweight, object-relational mapping tool for the Java programming language. It is a framework that simplifies the development of Java applications to interact with the database. Hibernate provides a reference implementation of JPA (Java Persistence API) for data persistence. It is used to develop persistence logic – to store and process the data for long use.
It provides an abstraction layer which means that the programmer does not have to worry about the implementations as they are handled internally. The implementations performed internally include establishing a connection with the database, writing a query to perform CRUD operations, etc.
- What is an ORM tool?
Ans. An ORM tool is a programming technique that maps the object that is stored in the database. It converts data between incompatible type systems in object-oriented programming languages such as Java, C#, etc. It simplifies data creation, data manipulation, and data access. An ORM tool internally uses the Java API to interact with the databases.
An ORM solution consists of the following components:
- API – for performing basic CRUD operations
- Language or API – for specifying queries that refer to classes and properties of classes
- Configurable facility – specify mapping metadata
- Technique – for interacting with transactional objects (dirty checking, lazy associations fetching)
- Name some databases supported by Hibernate.
Ans. Below some of the databases supported by Hibernate:
- MySQL
- PostgreSQL
- SQLServer
- Sybase SQL Server
- Oracle
- FrontBaase
- Informix Dynamic Server
- DB2/NT
- What are the advantages of Hibernate over JDBC?
Ans. Below are the advantages of Hibernate over JDBC:
- Object-relational impedance mismatch
Hibernate solves the object-relational impedance mismatch problems that occur when a relational database is connected by an application written in an object-oriented programming language style.
- Mapping Java classes to the database
JDBC requires you to write code to map the object model’s data representation to a relational model and its corresponding schema. Hibernate maps the Java classes to the database tables using XML configuration or by using annotations.
- Clean readable code
Hibernate helps in removing JDBC API-based boiler-plate codes. This makes the code cleaner and readable.
- HQL (Hibernate Query Language)
Hibernate uses HQL which is similar to SQL. HQL provides full support for polymorphic queries. It also supports native SQL statements.
- Database independent code
Hibernate’s database-independent code makes it easier to migrate to a new database. This is because you don’t need to change the HQL queries (apart from some exceptions) when you change the databases such as MySQL.
- Transaction Management
JDBC doesn’t support implicit transaction management. The developer has to write transaction management code using commit and rollback methods. In JDBC, if a transaction is successful, then you need to commit. If it is failed, you have to rollback the transaction using:
con.commit();
con.rollback();
In Hibernate, these transactions are implicitly provided.
- Caching Mechanism:
This mechanism in Hibernate reduces the number of hits as much as possible that your application makes with the database server.
- Exception Handling
JDBC throws a checked exception called SQLException which makes it mandatory for the developer to write try-catch blocks to handle this exception at compile time. On the other hand, Hibernate wraps the JDBC exceptions and throws unchecked exceptions like JDBCException or HibernateException. This avoids writing multiple try-catch blocks to handle exceptions.
- How to invoke stored procedures in hibernate?
Ans. We can invoke stored procedures in hibernate using code as below:
<sql-query name=”getEmployees” callable=”true”>
<return alias=”em” class=”Employee”>
<return-property name=”emp_id” column=”EMP_ID”/>
<return-property name=”e_name” column=”EMP_Name”/>
<return-property name=”e_dept” column=”EMP_Department”/>
{ ? = call selectEmployees() }
</return>
</sql-query>
- Name the two types of collections in Hibernate.
Ans. The two types of collections in Hibernate are:
- Sorted Collection
- Order Collection
Q7. How to disable hibernate second level cache?
Ans. There are three ways to disable hibernate second level cache
- Using CACHEMODE.IGNORE
- Setting use_second_level_cache as false.
- Using cache provider as org.hibernate.cache.NoCacheProvider
- What is Hibernate Configuration File?
Ans. Hibernate Configuration File named hibernate.cfg.xml is a configuration file that contains database-related configurations and session-related configurations. It provides information to Hibernate framework about where to find the mapping information which defines the mapping between Java classes and database tables. The hibernate configuration file is placed under the src/main/resource folder by default. The configuration file also provides information about database details like:
- Database URL
- Credentials
- Dialect information, etc.
- What is the difference between first level cache and second level cache?
Ans. The differences between first level cache and second level cache are:
First level cache | Second level cache |
The first level cache is maintained at the Session level. | It is maintained at the SessionFactory level. |
Enabled by default. | Not enabled by default. Has to be enabled explicitly. |
It is available only until the session is open. | Second level cache is available across all sessions. It is available through the application’s life cycle. |
- Name the different types of association mapping that are possible in hibernate.
Ans. There are four types of association mapping in hibernate:
- One to One
- One to Many
- Many to One
- Many to Many
- What is HQL? List some commonly used methods of the query interface.
Ans. HQL (Hibernate Query Language) is an object-oriented query language, similar to SQL. However, HQL is fully object-oriented. HQL works with persistent objects and their properties instead of tables and columns. HQL provides an efficient way for performing complex operations on relational databases without writing complicated queries.
Below are some commonly used methods of the query interface:
Method | Description |
public int executeUpdate() | to execute the update or delete query |
public List list() | returns the result as a list |
public Query setFirstResult(int rowNumber) | accepts the row number as parameter, using which the record of that row number would be retrieved |
public Query setMaxResult(int rowNumber) | specifies the number of records to be retrieved from the relation (table) |
public Query setParameter(int position, Object value) | sets the value to the attribute/column at a particular position (JDBC style query parameter) |
public Query setParameter(String name, Object value) | sets the value to a named query parameter |