Hibernate Theory
Hibernate Theory
FRAMEWORK
HIBERNATE FRAMEWORK
• The ORM tool internally uses the JDBC API to interact with the
database.
JPA
• Java Persistence API (JPA) is a Java specification(collection of classes
and interfaces) that provides certain functionality and standard to
ORM tools.
1) Open Source and Lightweight : Hibernate framework is open source under the LGPL license
and lightweight.
2) Fast Performance : The performance of hibernate framework is fast because cache is internally
used in hibernate framework.
3) Database Independent Query : Hibrenate supports Hibernate Query Language. HQL 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.
4) Automatic Database and Table Creation : Hibernate framework provides the facility to create
the tables and database automatically. So there is no need to create tables or database
manually.
For creating the a hibernate application, we need to
follow the following steps:
2. Do the mapping for Entity class using @Entity , @Id annotations etc.
4. Create the class that stores or retrieves the persistent object , i.e..
Class to perform database operations.
2. Map the class : Annotate the persistent class with @Entity annotation
to map the class with the table in database.
• The following image shows the class level architecture of JPA. It shows the
core classes and interfaces of JPA.
DATABASE
• Persistence : This class contains static methods , like
createEntityManagerFactory(String persistenceUnitName) to
obtain EntityManagerFactory instance. It accepts the persistence
unit name as input and loads all the properties from the
persistence file.
@Table : Used to change table details, such as name of the table. If you don't
use @Table annotation, hibernate will use the class name as the table name
by default.
@Id : marks the identifier for the entity. It is Used for declaring a primary
key inside our entity class.
<property name="hibernate.cache.use_second_level_cache"
value="true"></property>
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.ehcache.EhCacheRegionFactory">
</property>
MAPPING
• Mapping in a key feature which allows to establish a
relationship between two or more entities.
The second level cache stores data that is frequently used across different
sessions(entity managers), reducing the number of database queries and
improving the overall performance of the application.
• NOTE : The version of ehcache dependency and hibernate dependency must be same.