In this section we will understand important concepts on which Hibernate is based. It is essential to understand these things before deep diving further.
Entity and Value Type
Hibernate makes distinction between entity and value types. Entity types represent the objects that can be persisted to the database. They are the first class citizen in hibernate. Hibernate manages the life cycle of and entity. An entity also has an identifier which identifies the different instances uniquely. Value types are objects which are managed in the context of an entity. They live and die within the scope of an entity. This distinction comes because of Hibernate philosophy of promoting rich domain model. This means more classes per table. Let's say we have Student Table which contains the Address also
Student table
ID
NAME
CITY
COUNTRY
ID
NAME
CITY
COUNTRY
Now we can map this table to two class. One Entity and another value type
Student (entity class)
Student (entity class)
@Entity
public class Student{
private Long id;
private String name;
private Address address;
public class Student{
private Long id;
private String name;
private Address address;
//Setters and getters
Address (value class)
public class Address{
private String city;
private String country;
private String city;
private String country;
//setters and getters
}
}
So we have got two classes for one table. Also the life of Address object is within the life scope of Student object.The Student object can live independently but an independent address object has no meaning unless it is associated with a Student object.
Flushing
Flushing is the process of propagating the changes from the Java layer to the database. Whenever the call is done for a save, hibernate does not issues the SQL automatically. It keeps on noting the changes and queues all the SQL. The SQL's are issued to database at appropriate time. This propagation of changes is known as flushing. The default mode for flushing is automatic, where hibernate identifies when to flush. The flushing is done in the following cases:
- When the transaction is committed.
- Before query execution so that the query sees the latest data in the database.
- When session.flush() is called explicitly.
The flushing mode can be changed by calling session.setFlushMode with parameter FlushMode.COMMIT. In this case the flushing happens only
- When the transaction is committed.
- When session.flush() is called explicitly.
Dirty Checking
Hibernate automatically tracks the changes in the entities when they are in managed state and persists the changes to the database. Look at the following example
//Get the session from the Session factory. In the first unit of work we persist the Student object
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx= session.beginTransaction();
Student student = new Student();
student.setName("oyeJava");
Long studentId = (Long)session.save(student);
tx.commit();
session.close();
//Start another unit of work. Note that we fetch the same row that we have saved.
//Now change the name of student object but do not call any save or update method.
//Still hibernate will figure out that the object has changed and hibernate will
//persist the changes to the datbase.The process of finding the changes is known as dirty checking also.
session = HibernateUtil.getSessionFactory().openSession();
tx= session.beginTransaction();
Student student1 = (Student)session.get(Student.class, studentId);
student1.setName("Again Java");
tx.commit();
session.close();
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx= session.beginTransaction();
Student student = new Student();
student.setName("oyeJava");
Long studentId = (Long)session.save(student);
tx.commit();
session.close();
//Start another unit of work. Note that we fetch the same row that we have saved.
//Now change the name of student object but do not call any save or update method.
//Still hibernate will figure out that the object has changed and hibernate will
//persist the changes to the datbase.The process of finding the changes is known as dirty checking also.
session = HibernateUtil.getSessionFactory().openSession();
tx= session.beginTransaction();
Student student1 = (Student)session.get(Student.class, studentId);
student1.setName("Again Java");
tx.commit();
session.close();
Whenever hibernate fetches or saves an object to the database, it keeps a snapshot of the object. At the point when hibernate goes to persist the changes, it loops through all the objects and matches it with the snapshot it has got.If it identifies that the state of an entity has changed from its snapshot it flushes those changes to the database.
More write-ups on Hibernate
More write-ups on Hibernate
No comments:
Post a Comment