CORE COMPONENTS of HIBERNATE
- Configuration Object
- SesssionFactory Object
- Session Object
- Query Object
- Criteria Object
- Transaction Object
Hibernate uses well known existing Java API’s like JDBC, Java Transaction API (JTA) and Java Naming and Directory Interface (JNDI). But, you do not need to worry about these API’s, because when you program in Hibernate – hibernate will internally use those API.
1. CONFIGURATION OBJECT
It is the first object you create in the Hibernate and it is only created once during Hibernate application initialization. This configuration object will have information to connect to database and mapping information to connect the Java POJO classes to the Database tables.
Database Connection ( hibernate.cfg.xml ) : Database connection properties like Username and Password etc
Class Mapping ( *.hbm.xml or Annotations ) : To connect the Java POJO classes to the Database tables
You can also setup the Configuration object using java classes. But it is generally not preferred as setting configuration object in XML files is relatively much easier.
2. SESSSIONFACTORY OBJECT
The SessionFactory object is created using the Configuration object provided. Only one SessionFactory object per database is created during the application start. If your application has connection to multiple database then one SessionFactory object is created for each database.
SessionFactory object is thread safe object. SessionFactory will create a new Session object when requested.
3. SESSION OBJECT
The Session object will get physical connection to the database. Usually Session object is request each time some interaction needed with the database. The persistent objects can be retrieved and saved through the Session object.
1
2
3
4
5
6
7
8
|
Session session = sessionFactory.openSession();
Transaction tx = = session.beginTransaction();
// do some work
...
tx.commit();
session.close();
}
|
The Session object that is opened should be close as soon as the transaction is completed. As Session is not thread safe, they should be created and destroyed as soon as possible.
4. QUERY OBJECT
The Query object uses either Hibernate Query Language (HQL) queries or Native SQL queries to query the database.
1
2
|
Query hqlQuery = session.createQuery("SELECT E.firstname FROM Person" );
List resultsList = hqlQuery.list();
|
5. CRITERIA OBJECT
The Criteria object helps in creating and Object Oriented Criteria queries to do operations on database.
1
2
|
Criteria cr = session.createCriteria(person.class);
List resultsList = cr.list();
|
No comments:
Post a Comment