Hibernate
Basics:
Today
I am going to discuss on basic hibernate example:
Basic
files required to work with hibernate:
1. POJO
class.
2. Hibernate
mapping file
3. Hibernate
configuration file
4.
Client application
1. POJO class:
If
a class is not extending any pre-defined class or not implementing any
pre-defined interface given by API, such type of classes are called POJO class.
Example:
Class
Student
{
private String StudentName;
private String StudentName;
Private
int StudentNumber;
----
//setters
and getters for above variables
}
2. Hibernate mapping file:
Hibernate
mapping file is an XML file, which contains association between POJO class
name to database table name and POJO
class member variables to database table column names. Mapping file name we to give as <anyname>.hbm.xml, here hbm
is to identify the file is hibernate mapping file. We can give any name .xml
file, but it is not recommended to use <anyname>.hbm.xml.
Example
1. Student.xml
2. Student.hbm.xml
1st
& 2nd correct but 2nd
one is recommended
Student.hbm.xml
<!
DTD is must and should>
<hibernate-mapping>
<calss
name=”Student” table=”STUDENT”>
<id
name=”studentId” column =”sid”>
<generator
class=”assigned”/>
</id>
<property
name=”studentName” column name=”SNAME”>
<property
name=”studentNumber” column name=”SNO”>
</class>
</hibernate-mapping>
3. Configuration file
Hibernate
need some additional properties along with mapping file, in order to transfer
the objects between java application and database.
In configuration
file we will add the following information.
1.
connection properties-hibernate will establish connection with database
2. hibernate
properties-adding additional information about database
3. mapping
resources-include hibernate mapping file in hibernate configuration file.
When
hibernate configuration file is loaded automatically hibernate mapping file will
also load.Hibernate
configuration file is identified with
<any-name>.cfg.xml file. Here cfg specifies it is hibernate configuration
file.
<any-name>.cfg.xml
<!DTD
must>
<hibernate-configuration>
<session-factory>
<!-
-connection properties-->
<property
name="connection.driver_class">Driver Class Name </property>
<property
name="connection.url">URL </property>
<property
name="connection.user">user </property>
<property
name="connection.password">password</property>
<!-
-hibernate properties-->
<property
name="hibernate.show_sql">true/false</property>
<property
name="hibernate.dialet">Database dialet class</property>
<property
name=" hibernate.hbm2ddl.auto">create/update or what
ever</property>
<!-
-mapping resources-->
<mapping
resource="hbm file1 .xml" / >
<mapping
resource=" hbm file2.xml" / >
</session-factory>
</hibernate-configuration>
For
each database we have to create separate configuration file, i.e if we have 3
databases we need to create 3 hibernate configuration files.
4. Client application
Step1:
Import the hibernate API in to client application.
Import
org.hibernate.*;
Import
org.hibernate.cfg.*;
Step2:
Create
an object for Configuration class, by creating an object for Configuration class we are boot strapping hibernate
environment into a java application.
Configuration
cfg=new Configuration();
Step
3:
Call
configure() method of configuration class, for loading hibernate configuration
class.
If the
hibernate configuration file name is hibernate.cfg.xml then the file name is
optional. When configure() method is called
then hibernate internally uses DOM and SAX parsers to read both
configuration and mapping file and stores that data in the form of instance
variables of the configuration class.
Step
4:
Create
high level object of hibernate called
SessionFactory. To get SessionFactory object
we need to call buildSessionFactory() method of configuration class.
SessionFactory
factory=cfg. buildSessionFactory();
Step
5:
1. Create
or get Session object.
2. SessionFactory
produces Sessions and Session is the runtime interface between a java application
and hibernate frame work.
3.
all methods to perform operations on data base are provided by Session object.
Session
session=factory.OpenSession();
Step
6:
Begin
or open a logic for transaction, while performing select ,update,delete operations from a java application to a DB, in hibernate we need transaction.if
we are loading or reading data from the database we don’t require transaction. To
get transaction in hibernate we need to
call beginTransaction() given by Session
interface.
Transaction
tx=Session.beginTransaction();
Step
7:
Perform
the operations on the database while
using the methods of Session interface.
Session.save(obj);
Session.update(obj);
Session.delete(obj);
Step
8:
Commit
the transaction
Tx.commit();
Step
9:
Close
the session and session factory objects.
Session.close();
Factory.close();