Hibernate Concepts
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 withing 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
Now we can map this table to two class. One Entity and another value type
Student (entity class)
@Entity 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; //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();
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.
Back to Hibernate Index
Back To Java Home
Back To Home
Sidebar
Last wiki comments
- AOP: Thanks
- Lalit Bhatt: Superb Collection
- Lalit Bhatt: J2EE training
- Introduction to ORM: timberland shoes
- Introduction to ORM: jordan shoes
- Introduction to ORM: nike air max
- Pune Tourist Spots: KONARK PARK CLOSED
- jQuery Form Validations: Jquery Developer
- Spring Introduction: RG
- SOAP: Re: Assertion
Sidebar
Random Pages
- Comprehensive Overview of Java Technologies
- SOAP
- Pune Hill Forts For Trekking
- Windows Antivirus
- Source Code Control Systems
- JavaFX Data Binding and Trigger
- Introduction to Seam
- CSS - Handling Browser Moods
- Parsers Introduction
- Spring Annotation driven Aspects
- JAXB Validation using Schema
- Spring POJO Aspects
- What markets work on?
- Why projects fail?
- Bharat Band - Jai ho
- The concept of Nation
- Don't hide complexity if it cannot be handled in a robustway
Last blog post comments
-
Bharat Band - Jai ho: How do we protest?
Wed 18 of Aug., 2010 13:13 IST
-
Divided by Destiny: Contact
Fri 23 of July, 2010 16:02 IST
-
Future of Java: thesis writing
Sat 17 of July, 2010 01:50 IST
-
Hang till Death Mr. Kasab: some change
Mon 28 of June, 2010 16:03 IST
-
God Religion : Why we are confused?: Re: Is GOD Necessary?
Tue 15 of June, 2010 17:29 IST
-
God Religion : Why we are confused?: Is GOD Necessary?
Tue 15 of June, 2010 13:06 IST
-
The reason in religion: good
Wed 10 of Mar., 2010 18:30 IST
-
The confusion of Design Patterns: I think at macro level you are right...
Tue 23 of Feb., 2010 03:31 IST
-
The Indian Municipality: Comment
Fri 22 of Jan., 2010 13:20 IST
-
What Government should do?: Re: Review of the Indian Law and Order and Justice Dispensation regime.
Fri 22 of Jan., 2010 13:16 IST
Post new comment