Hibernate - Introduction: Services Limited
Hibernate - Introduction: Services Limited
Introduction
Hibernate framework simplifies the development of
java application to interact with the database.
Hibernate is an open source, lightweight,ORM
(ObjectRelationalMapping) tool.
An ORM tool simplifies the data creation, data
manipulation and data access. It is a programming
technique that maps the object to the data stored in
the database.
Hibernate Framework
Advantages
There are many advantages of Hibernate Framework.
1)OpensourceandLightweight: Hibernate framework is
open source under the LGPL license and lightweight.
2)Fastperformance: The performance of hibernate
framework is fast because cache is internally used in hibernate
framework. There are two types of cache in hibernate
framework first level cache and second level cache. First level
cache is enabled by default.
Advantages
3)DatabaseIndependentquery: HQL (Hibernate Query
Language) is the object-oriented version of SQL. It generates
the database independent queries. So you don't need to write
database specific queries. Before Hibernate, If database is
changed for the project, we need to change the SQL query as
well that leads to the maintenance problem.
4)Automatictablecreation: Hibernate framework provides
the facility to create the tables of the database automatically.
So there is no need to create tables in the database manually.
Advantages
HibernateArchitecture
The Hibernate architecture includes many objects persistent
object, session factory, transaction factory, connection factory,
session, transaction etc.
There are 4 layers in hibernate architecture java application
layer, hibernate framework layer, backhand api layer and
database layer. Let's see the diagram of hibernate
architecture:
HibernateArchitecture
This is the high level architecture of Hibernate with mapping file and configuration file.
8
HibernateArchitecture
Hibernate framework uses many objects session factory, session, transaction etc.
along with existing Java API such as JDBC (Java Database Connectivity), JTA (Java
Transaction API) and JNDI (Java Naming Directory Interface)
9
10
11
Mapping
Twotypes:
XML Mapping
Annotation Mapping
12
HibernatewithAnnotation
13
14
Addthejarfilefororacleandannotation
For oracle you need to add ojdbc14.jar file. For using, you
need to add:
hibernate-commons-annotations.jar
ejb3-persistence.jar
hibernate-annotations.jar
15
CreatethePersistentclass
@Entity annotation marks this class as an entity
@Table annotation specifies the table name where data of this
entity is to be persisted. If you don't use @Table annotation,
hibernate will use the class name as the table name by default.
@Id annotation marks the identifier for this entity.
@Column annotation specifies the details of the column for this
property or field. If @Column annotation is not specified, property
name will be used as the column name by default.
16
AddmappingofPersistentclassinconfiguration
file
open the hibernate.cgf.xml file, and add an entry of mapping
resource like this:
<mapping class=location of the model class in the project"/>
17
HibernateProperties
hibernate.dialect : This property makes Hibernate generate the
appropriate SQL for the chosen database.
hibernate.connection.driver_class : The JDBC driver class.
hibernate.connection.url : The JDBC URL to the database instance.
hibernate.connection.username: The database username.
hibernate.connection.password : The database password.
hibernate.connection.pool_size : Limits the number of connections
waiting in the Hibernate database connection pool.
18
WhatisDialects?
Dialect is a "helper" for Hibernate to communicate with the
database in its language.
Dialect in hibernate converts any query into database specific
query, regardless of the database type. And Hibernate supports
HQL queries, internally these HQL queries will be converted
into native Database SQL calls.
19
SQLDialectsinHibernate
For connecting any hibernate application with the database,
you must specify the SQL dialects. There are many Dialects
classes defined for RDBMS in the org.hibernate.dialect
package. They are as follows:
20
Thank You
21