Target AudienceThis tutorial is designed for Java programmers who need to understand the Hibernate framework and its application.
Prerequisites:Before proceeding with this tutorial you should have a good understanding of the Java programming language and also good understanding of SQL.
This is 1 of 8 parts of tutorial series
- Introduction to hibernate framework
Hibernate hello world example in eclipse
Hibernate one to one mapping example
Hibernate one to many mapping example
Hibernate many to many mapping example
Hibernate inheritance:Table per class hierarchy
Hibernate inheritance:table per subclass
Hibernate inheritance:Table per concrete class
What is ORM?ORM is a programming method to map the objects in java with the relational entities in the database.In this,entities/classes refers to table in database,instance of classes refers to rows and attributes of instances of classes refers to column of table in database.This provides solutions to the problems arise while developing persistence applications using traditional JDBC method. This also reduces the code that needs to be written.
Need for tools like hibernate:
The main advantage of ORM like hibernate is that it shields developers from messy SQL. Apart from this, ORM provides following benefits:
- High-level object-oriented API
- Less Java code to write
- No SQL to write
- Sophisticated caching
- Lazy loading
- Eager loading
- A lot less code to write
- ORM framework generates database-specific SQL for you
What is hibernate?
Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables.The main goal of hibernate is to relieve the developer from the common data persistence related tasks.It maps the objects in the java with the tables in the database very efficiently and also you can get maximum using its data query and retrieval facilities.Mainly by using Hibernate in your projects you can save incredible time and effort.
Architecture of hibernate :
Following is a detailed view of the Hibernate Application Architecture with few important core classes.
The Hibernate architecture is layered to keep you isolated from having to know the underlying APIs.Hibernate is like a bridge between java application and relational database.
Core classes of hibernate are:
Core classes of hibernate are:
This is the primary interface used by hibernate applications. The instances of this interface are lightweight and are inexpensive to create and destroy. Hibernate sessions are not thread safe.It allows you to create query objects to retrieve persistent objects.It wraps JDBC connection Factory for Transaction.It holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier .
SessionFactory Interface :This is a factory that delivers the session objects to hibernate application.It is a heavy weighted object so generally there will be a single SessionFactory for the whole application and it will be shared among all the application threads.The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work.
Configuration configuration=new Configuration(); configuration.configure(); ServiceRegistry sr= new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); SessionFactory sf=configuration.buildSessionFactory(sr);SessionFactory object is created with the help of configuration object.
Configuration Interface :
This is used to configure hibernate. It’s also used to bootstrap hibernate. Mapping documents of hibernate are located using this interface.