What is Hibernate?

- Hibernate is an ORM (Object Relational Mapping) and persistent framework.

- The framework helps to map plain java object to relational database table using xml configuration file.

- The framework helps to perform following things.

1. Perform basic CURD operations.

2. Write queries referring to java classes (HQL queries).

3. Facilities to specify metadata.

4. Dirty checking, lazy association fetching.

Why hibernate and how does it help in the programming?

The main advantage of Hibernate (ORM) framework is that it shields developer to write a messy SQL. Apart from that ORM provides following benefits.

1. Improve Productivity of the developer by providing high level object oriented API (e.g. API for easily maintaining the connection to data base, mapping java classes to relational database tables), less java code to write, helps to avoid writing SQL query.

2. Improved performance by providing sophisticated caching, lazy loading and eager loading features.

3. Provide portability, the framework helps to generate database specific SQL for you.

How will you configure Hibernate?

- To configure hibernate, you need hibernate.cfg.xml or hibernate.properties file and *.hbm.xml files, all these files are used by configuration class to create sessionFactory, which in turn creates the session instances.

- Session instances are the primary interface for persistence services.

- The hibernate.cfg.xml or hibernate.properties files are used to configure the hibernate service (database connection driver class, database connection URL, connection username, connection password, dialect, mapping resources etc.).

- The *hbm.xml files are used for mapping persistent objects to relational database.

- From Java 5 onwards you can configure and map persistent objects through annotations.

Which settings will be loaded if both hibernate.properties and hibernat.cf.xml files are present in the classpath?

If both hibernate.properties and hibernate.cfg.xml files are present in the classpath then hibernate.cfg.xml file will override the settings found in hibernate.properties. So please make sure that your project should include either hibernate.properties or hibernate.cfg.xml file.

What are the Core interfaces of Hibernate framework?

There are five core interfaces being used extensively in every Hibernate application. Using these interfaces you can store or retrieve any persistent objects and also control transactions.

1. Session interface

2. SessionFactory interface

3. Configuration interface

4. Transaction interface

5. Query and Criteria interfaces

What is the role of Session interface in Hibernate?

The session interface is the primary interface used in Hibernate Application.

- It is a single threaded sort-lived object and represents conversation between application and the persistent store.

- It helps to create query objects to retrieve persistent objects.

- You can get the session object from session factory :

Session session = sessionFactory.openSession();

- Session Interface role :

1. Wraps a JDBC connection.

2. Factory for Transaction.

3. Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier.

What is the role of SessionFactory?

The application obtains session object from SessionFactory interface. Typically there should be only one sessionFacory for whole application and is loaded during application initialization. The SessionFactory caches generate SQL Statement and other mapping metadata that Hibernate use at runtime. It also hold cached data that has been read in one unit of work and can be reused in a future unit of work.

You can get the instance of SessionFactory by the configuration object as below :

SessionFactory sessionFactory = configuration.buildSessionFactory();

How do you implement one to one relationship in Hibernate with XML mapping?

- For example you have table emp(employee table) and emp_detail(employee details) and assuming there is a one to one relationship between them. For the above tables you have to create corresponding POJO classes and hbm.xml files.

- So for emp table the java class is Employee.java with property empId and xml file is emp.hbm.xml.

- And for emp_details the java class is EmployeeDetail.java (properties are name, address etc.) and xml file is empdetail.hbm.xml.

What are the Collection types in Hibernate?

1. Bag

2. Set

3. List

4. Array

5. Map

What is HibernateTemplate?

The spring framework provides HibernateTemplate(org.springframework.orm.hibernate.HibernateTemplate) which is kind of helper class and provides following benefits.

1. HibernateTemplate class simplifies interaction with Hibernate session.

2. Common functions are simplified to single method calls.

3. Sessions are automatically closed.

4. Exception are automatically caught and converted to runtime exceptions.

What is the difference between load() and get() method?

load() :

1. Use load() method only when you are sure that object you want to read already exists.

2. If unique Id of an object does not exists in database then load() method will throw an exception.

3. load() method return proxy object default and database wonot be hit until the proxy is first invoked

get() :

Use get() method when you are not sure obout the object existance in the database. :

If object does not exists in the database, the get() method will return null.

get() method will hit database immediately.

What is lazy fetching in Hibernate?

In Hibernate Lazy fetching is associated with child objects loading for its parents. Through Hibernate mapping file (.hbm.xml) you can specified the selection of loading child objects. By default Hibernate does not load child objects. Lazy=true means not to load the child objects.

What is the difference between merge and update method?

Use update() method when you are sure that session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session.

What are the different types of caches in Hibernate?

Hibernate uses two different type of caches for objects: first-level cache and second-level cache. First level of cache is associated with Session object, while second-level of cache is associated with the SessionFactory object. By default, Hibernate uses first-level of cache on a per-transaction basis. Hibernate mainly use this cache to reduce the number of SQL queries it needs to generate within a given transaction.

How does Hibernate useful in mapping object and its relations?

- Hibernate is a framework for Java that is used to map the object and its relations.

- It is used to build persistent objects using the terminologies of JAVA.

- It uses the following properties to perform the function of mapping :

1. Association : Where the one object can be associated with another object of the same or different class.

2. Inheritance : It is a property of inheriting the properties and features of the parent class.

3. Polymorphism : It is a feature that allows more than one data type to be handled by a single function.

4. Composition : It is used to combine the simple data types into complex data types.

What is the function performed by code generator in Hibernate?

Code generator is a framework that allow simple and easy mapping of objects using pre-defined tags. It follows Model driven architecture which uses models of the objects separately and link them with each other. Code generator adds attributes to the source code. I uses the java-doc tags to generate the hibernate mapping. It is also used to generate the database definition language from the source code to map the objects correctly. It allows easy mapping of objects from an existing database and allow it to be connected to some different database model as well.

What is the need of hibernate tools when ORM tools can be used?

The use of hibernate tools is to allow the programmer to code efficiently and saves them from being involved with the SQL code directly. It is used to increase the productivity of the work and uses high level object oriented APIs to increase the efficiency and performance of the work. It allows the programming to be more objects oriented and the code in Java has to be written less. It also save programmers time to write the SQL code as there is no need to write the SQL code in it. These tools provide very good caching system that keeps all the files and they provide it when it is required. It allows easy maintenance of the overall system and the code that is being written in it.

What is the role played by Session interface in Hibernate?

Session interface is the primary interface used by Hibernate. It is the important interface for the applications that uses single thread, and a short lived object representing a conversation between application and the objects that are stored. This object allows creating query objects that can be used to call persistent objects from the database. This interface allows the wrapping of JDBC connection and keeps the transactions at one place. It holds the persistent objects in the cache for easy navigation and to increase the performance of the overall system. The interface is represented as :

Session session = sessionFactory.openSession();

What is the role played by SessionFactory interface in Hibernate?

The session instance is being obtained by the application from SessionFactory. It keeps the session instances at one place for easy access and dynamic allocation of the resources. There is always a single SessionFactory created for the complete application. This is created during the application initialization. It creates the cache that is used to generate the SQL statements and metadata information about the mapping of the Hibernate functions that gets used in runtime. It holds the data that has been cached and then read into it. It focuses on reusability of the work and the functions that are stored in the SessionFactory. This is represented as :

SessionFactory sessionFactory = configuration.buildSessionFactory();

What is the process of communication between Hibernate with RDBMS?

To create the communication between the Hibernate and RDBMS there are some steps that are involved in the process. These are as follows : - To create the communication first loading of the Hibernate configuration file takes place. When this file is loaded then a configuration object gets created. This object loads the .hbm files used to map the objects automatically. - A session gets created by taking the interface from the SessionFactory from the configuration object. - A HQL query is being creation after taking the session from the session factory and starting the session for the user. - In the end an execution of the query gets listed that contain the Java Objects of the file. This allows the communication being done between different objects of Hibernate and RDBMS.

What is the purpose of using HibernateTemplate?

HibernateTemplate is a spring template that is used as a class to simplify the interaction with the Hibernate Session. HibernatTemplate includes simplified single method calls that are used to close the sessions automatically. It includes the exception handler that catches the exception automatically and converted it in runtime. There are classes used in Hibernate that provides different methods for query and to retrieve the data from the database. The helper class that is provided is org.springframework.orm.hibernate. HibernatTemplat is used to check and convert the HibernateExceptions into unchecked DataAccessExceptions.

Why Hibernate is preferred over JDBC?

The preference of Hibernate is more due to the transparency it provides to the programmer for writing their code. It doesn?t allow the programmer to write explicit code to map the database tables to the application objects that are used during the interaction with RDBMS. It provides an advanced ORM solution to map the Java class files to the database tables for easy access and retrieval of the data. Hibernate uses Hibernate Query Language that is independent of the type of database used. It allows polymorphic queries to be retrieved and selects the way to perform the database manipulation tasks. Hibernate allow the storing of large amount of data in small files that can be easily accessed and the performance can be faster. It also provides the actually mapping to be performed between the tables and objects in the XML files.

What is the use of Hibernate proxy in Hibernate?

The proxies are easy to create and it is created dynamically by sub-classing the objects at runtime. This sub-class consists of all the parent methods and allows easy accessing of them. The proxy allows the loading of this object in real time from the database and calls the methods from there. Proxy is used as a mechanism in Hibernate allowing breaking of the objects in the database in smaller parts that can easily be accessed and use the memory efficiently. It is easy for a class to be mapped to a proxy than to a table. When there is a call load on a sessioin then the value that is returned is the proxy. The proxy consists of the methods to load the data. The sample code to call the proxy is given as :

Link
Eng. Ravi Kumar Verma

Simple and dept learning technology is very important part in software development. Same story has been implemented at this site, so learn and implement the inovative things, Thanks

Ravi Kumar Verma