Hibernatenotes
Hibernatenotes
• ORM Framework ORM stands for Object Relation Mapping. It is a middleware tool that lies
between the application and database. It wraps the implementation ...
HIBERNATE:-
• Hibernate is a ORM-Tool and java framework used to interact (communicate) with the
database.. It is an open-source, lightweight, ORM(Object Relational Mapping) tool.
Hibernate implements the specifications of JPA (Java Persistence Api) ….
ADVANTAGES OF HIBERNATE:-
EntityManager interface is used to allow applications to manage and search for entities
in the relational database. We can save, update and delete the data in database
IMPORTANT METHODS :
• persist – Make an instance managed and persistent.
• merge – Merge the state of the given entity into the current persistence context.
• IMPORTANT METHODS :
• begin() method - This method is used to start the transaction.
Syntax :
EntityManagerFactory emf = Persistence.createEntityManagerFactory(“String”);//string =
persistence unit name
EntityManager em1 = EntityManagerFactory.createEntityManager();
EntityTransaction et = EntityManager.getTransaction();
DIFFERENCE BETWEEN PERSIST AND MERGE
Persist Merge
Persist() will only save data in the Merge() will save the data and update
database the data
If we pass the object with duplicate If we pass the object with duplicate
primary key, it will throw an exception primary key, if primary key matches in
the table it update the data , if it does
not matches it inserts the data
MAPPING IN HIBERNATE
• The mapping between entity classes and the relationships between tables is the soul of
ORM.
• hibernate mappings are one of the key features of hibernate. they establish the
relationship between two database tables.
• you can establish either unidirectional or bidirectional..
DIFFERENT MAPPING ANNOTATIONS ARE :
• @OneToOne(uni-direction)
• @OneToMany(uni-direction)
• @ManyToOne(uni-direction)
• @ManyToMany(uni-direction)
• @OneToOne(bi-direction)
• @OneToMany(bi-direction)
• @ManyToMany(bi-direction)
ONE-TO-ONE MAPPING
• Cascading is a feature in Hibernate, which is used to manage the state of the mapped
entity whenever the state of its relationship owner (superclass) affected. When the
relationship owner (superclass) is saved/ deleted, then the mapped entity associated
with it should also be saved/ deleted automatically.
• When we perform some action on the target entity, the same action will be applied to
the associated entity.
TYPES OF CASCADING
PERSIST
MERGE
REMOVE
CACHING IN HIBERNATE
• Hibernate caching improves the performance of the application by saving the object in
the cache. It is useful when we have to fetch the same data multiple times.
• There are mainly two types of caching:
o First Level Cache, and
o Second Level Cache
SECOND LEVEL CACHING
• SessionFactory object holds the second level cache data. The data stored in the second
level cache will be available to entire application. But we need to enable it explicitely.
• Three steps to enable second level cache :
• 1.Add hibernate-echache dependency . (version should be same as hibernate version)
• 2.Add @cacheble annotation to Entity class.
• 3.Enable second-level cache by configuring in persistence.xml file.