SlideShare utilise les cookies pour améliorer les fonctionnalités et les performances, et également pour vous montrer des publicités pertinentes. Si vous continuez à naviguer sur ce site, vous acceptez l’utilisation de cookies. Consultez nos Conditions d’utilisation et notre Politique de confidentialité.
SlideShare utilise les cookies pour améliorer les fonctionnalités et les performances, et également pour vous montrer des publicités pertinentes. Si vous continuez à naviguer sur ce site, vous acceptez l’utilisation de cookies. Consultez notre Politique de confidentialité et nos Conditions d’utilisation pour en savoir plus.
What is ORM ?
● ORM handles object relational impedance mis-match .
● Relational Database is table driven (with rows and column) for fast query operation
whereas java code is made up with object and classes of this is mis-match in
between java code and relational database .
● Like there is different tend to define data e.g for numeric value NUMBER in oracle
● ORM handles this mis-match for you.
● If you are not using any ORM tool so you have to do all these mapping and for large
application it could be complex .
What is Hibernate ?
● Hibernate is most popular ORM framework and it and it let you work without being constrained
by table driven relational database model .
● Is handles relational mis-match.
● And in addition to its own native API hibernate also implements JPA API specification . As
such it can be use in any environment .
● You do not need to work with JDBC .
● So the core of hibernate is all about making persisting data easier .
● The following diagram describe high level
architecture of hibernate
● In this diagram the hibernate is using
Persistence.xml file for the configuration.
● And at the bottom there is Database which
is managed by hibernate connection
● Database is most expensive part because
it require lots of resource to open and
● Its standard configuration file in JPA it has to be include in the
META-INF folder in class directory .
● This file specify that which underline database is suppose to save,
update and remove the entity objects .
● This file configure for cache.
● This file configure for object relational mapping .
● Domain classes are class in an application that implement the entity of business
● An entity is a plain old java object (POJO) and formally it also called as entity
● This class represents a table in the database and instance represents row in that
○ Annotation with the javax.persistence.Entity annotation
○ Public or protected non argument constructor
○ The class must not be declare final
○ No instance must not be declare final .
● Persistence context is the set of managed
objects of entity manager that exist in
particular data store.
● At runtime whenever a session is opened and
closed, between those open and close
boundaries Hibernate maintains the object in a
Persistence Context. Think of it like a first level
runtime cache which hibernate controls.
● If an object that has to be retrieved and already
exists in the persistence context, the existing
managed object is returned without actually
accessing the database
● An instance of the domain class that means domain object can be in one of three states like .
1. Transient state
2. Persistence state
3. Removed state
4. Detached state
● Saving Objects
○ It’s most common operation that we perform in hibernate
○ By using persist method we can save objects.
● Retrieving objects
○ When we working with any sort of ORM or persistence framework we always need to return entity from the
database it can be list or a single entity .
○ So we gonna look how we do it with JPA.
● Updating Object
○ Here we see that how to modify persisted instance.
● Removing Objects
○ Here we remove entity from Database so for that in EntityManager we have remove method by
When we build an application we are not only writing data to a single table within a data model ,
entities are related to each other in different ways.
Following are the four ways in which the cardinality of the relationship between the
objects can be expressed. An association mapping can be unidirectional as well as
→ A one-to-one relationships occurs when one entity is related to exactly one occurrence in another entity.
→ In this relationship, one object of the one pojo class contains association with one object of the another
This relationship is supposing a user has only one credential.The navigation is one-way from the credential to the user : it’s possible to know
Credential of a User, but not vice-versa.
→ A one-to-many association is the most common kind of association where an Object can be associated with
multiple objects. For example a same Department object can be associated with multiple employee objects.
→ If the persistent class has list object that contains the entity reference, we need to use one-to-many association to map the
Employee and Department table hold One-to-many relationship. Each Department can be associated with
multiple Employees and each Employee can have only one Department.
→ A logical data relationship in which the value of one data element can exist in combination with many values of another
data element, and vice versa.
A many-to-many relationship refers to a relationship between tables in a database when a parent row in one table contains
several child rows in the second table, and vice versa.
→ We are using Employee-Meeting relationship as a many to many relationship example. Each Employee
can attain more than one meetings and each meetings can have more than one employee
→ Java is an object oriented language and It is possible to implement Inheritance in java and which one of
the most visible Object-relational mismatch in Relational model. Object oriented systems can model both “is a”
and “has a” relationship but Relational model supports only “has a” relationship between two entities.
Hibernate can help you to map such Objects with relational tables. But we need to choose certain mapping
strategy based on your needs.
There are three types of inheritance mapping in hibernate
1. Table per concrete class
2. Table per class hierarchy(Single Table Strategy)
3. Table per subclass
Table Per Class Hierarchy
In One Table per Class Hierarchy scheme, we store all the class hierarchy in a single table. A discriminator is
a key to uniquely identify the base type of the class hierarchy.
Following are the advantages and disadvantages of One Table per Class Hierarchy scheme.
● This hierarchy offers the best performance since single select may suffice.
● Only one table to deal with.
● Performance wise better than all strategies because no joins need to be performed.
● Changes to members of the hierarchy required column to be altered, added or removed from the table.
● Most of the column of table are nullable so the NOT NULL constraint cannot be applied.
● Tables are not normalized.
One Table per Concrete Class
In case of Table Per Concrete class, tables are created per class and there are no
nullable values in the table. Disadvantage of this approach is that duplicate
columns are created in the subclass tables.
In this case let's say our Person class is abstract and Employee and Owner are concrete classes. So the table
structure that comes out is basically one table for Owner and one table for Employee. The data for Person is
duplicated in both the tables.
Following are the advantages and disadvantages of One Table per Subclass scheme
This is the easiest method of Inheritance mapping to implement.
● Possible to define NOT NULL constraints on the table.
● Disadvantage of this approach is that duplicate columns are created in the sub tables.
● Changes to a parent class is reflected to large number of tables
● Tables are not normalized.
One Table Per Subclass
Suppose we have a class Person with subclass Employee and Owner. Following the class diagram and
relationship of these classes.
In One Table per Subclass scheme, each class persist the data in its own separate table. Thus in this one we have 3 tables;
PERSON, EMPLOYEE and OWNER to persist the class data. But a foreign key relationship exists between the sub class
tables and superclass table. So the common data is stored in PERSON table and subclass specific fields are stored in
EMPLOYEE and OWNER tables.
● Tables are normalized.
● Able to define NOT NULL constraint.
● It works well with shallow hierarchy.
● As the hierarchy grows, it may result in poor performance.
Caching is all about application performance optimization and it exists between your application and the
database to avoid the number of database hits as many as possible to give a better performance for
performance critical applications.
Hibernate second level cache uses a common cache for all the entityManager object of an Entity Manager Factory. It is useful
if you have multiple session objects from a session factory.
EntityManagerFactory holds the second level cache data. It is global for all the session objects and not enabled by default.