ORM - Hibernate
ORM - Hibernate
Developed in 2001 by Gavin King, Hibernate was introduced as a groundbreaking alternative to the EJB2-style entity bean approach.
Hibernate is java based ORM tool that provides framework for mapping application domain objects to the relational database tables and vice versa, also provides data query and recovery facilities.
By mapping Java objects to database tables, it streamlines data persistence and retrieval without the need for complex SQL queries.
Features: Automatic Transaction Management and Caching
What is JPA?
Java Persistence API (JPA) is a Java specification that provides specific functionality and is a standard for ORM tools.
javax.persistence package contains the JPA classes and interfaces.
Hibernate is an implementation of JPA guidelines.
Hibernate is described in org.hibernate package.
Hibernate is an Object-Relational Mapping (ORM) tool that is used to save the Java objects in the relational database system.
Hibernate Lifecycle
Transient State - When instantiated an object of a POJO class using the new operator then the object is in the transient state.
Persistent State - The object is connected with the Hibernate Session then the object moves into the Persistent State.
Detached State - Either have to close the session or we have to clear its cache, the object is in Detached State.
Removed State - when the entity object is deleted from the database then the entity object is known to be in the removed state.
Hibernate – Criteria Queries
Hibernate follows one method via Criteria API. This will help you to filter the resultset as desired. i.e. exactly how we write a “WHERE” clause for a SQL,
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria geekEmployeeCriteria = session.createCriteria(GeekEmployee.class);
List geekEmployeeList = geekEmployeeCriteria.list();
session.save(...);
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from MyEntity where name=:name");
query.setParameter("name", "John");
query.setCacheable(true);
query.setCacheRegion("myQueryCache");
tx.commit();
session.close();
Hibernate – get() and load() Method
get() method:
get() method is used to retrieve a persistent object from the database. It is a member of the Session interface, and it takes the class of the object to be retrieved and the primary key of the object as arguments.
get() method only hits the database if the object is not present in the session cache. If the same object is already present in the cache then it returns previously stored data from the cache.
get() method returns null if there is no object present in the database.
load() method:
load() method is used to retrieve an object from the database by its identifier (primary key). It is used to initialize a proxy object instead of a fully-initialized object, so it can be used to lazily load the object when it is needed.
load() method does not retrieve the object from the database when it is called. Instead, it returns a proxy object that represents the object. The actual object is only retrieved from the database when it is needed, such as when a method of the object is called or a property is accessed. This technique is known as “lazy loading” and it is used to improve the performance of Hibernate by avoiding unnecessary database queries.
load() method throws ObjectNotFoundException if there is no object found in the database.
Hibernate – Caching
Caching in Hibernate refers to the technique of storing frequently accessed data in memory to improve the performance of an application that uses Hibernate as an Object-Relational Mapping (ORM) framework. Hibernate provides two levels of caching:
First-Level Cache:
Hibernate uses a session-level cache, also known as a first-level cache, to store the data that is currently being used by a specific session. When an entity is loaded or updated for the first time in a session, it is stored in the session-level cache. Any subsequent request to fetch the same entity within the same session will be served from the cache, avoiding a database round-trip. The session-level cache is enabled by default and cannot be disabled.
Second-Level Cache:
Hibernate also supports a second-level cache, which is a shared cache across multiple sessions. This cache stores data that is frequently used across different sessions, reducing the number of database queries and improving the overall performance of the application. The second-level cache can be configured with various caching providers, such as Ehcache, Infinispan, and Hazelcast.
Hibernate – Different Cascade Types
Cascading is a feature in Hibernate
Cascading refers to the ability to automatically propagate the state of an entity (i.e., an instance of a mapped class) across associations between entities.
Cascading in Hibernate refers to the automatic persistence of related entities.
Cascading can be configured using annotations.
CascadeType.ALL
CascadeType.PERSIST
CascadeType.MERGE
CascadeType.REMOVE
CascadeType.REFRESH
CascadeType.DETACH
CascadeType.REPLICATE
CascadeType.SAVE_UPDATE
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
Hibernate – Different Cascade Types
Cascading is a feature in Hibernate
Cascading refers to the ability to automatically propagate the state of an entity (i.e., an instance of a mapped class) across associations between entities.
Cascading in Hibernate refers to the automatic persistence of related entities.
Cascading can be configured using annotations.
CascadeType.ALL
CascadeType.PERSIST
CascadeType.MERGE
CascadeType.REMOVE
CascadeType.REFRESH
CascadeType.DETACH
CascadeType.REPLICATE
CascadeType.SAVE_UPDATE
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
Comments
Post a Comment