You are on page 1of 7

HIBERNATE

Hibernate is ORM (Object Relational Object). Hibernate is Free to use, no license required. Hibernate is a Back End Framework. Hibernate is used to replace JDBC API. Uses the Hibernate Query Language (HQL) instead of SQL. Benefits: 1. 2. 3. 4. Uses ORM mapping. Simple Development Steps. All updates are happening by Transaction. Supports both Eager & Lazy Loading. a. Eager Loading loads data from DB when the 1st request is called. b. Lazy Loading keeps checking & when user uses the data then it loads. 5. Supports 2 Types of Cache Management: a. First Level Cache Management. b. Second Level Cache Management. 6. Supports several levels of auto-generated incremented values. 7. Supports all types of DB available in the markets & easy to change from one database to other (i.e., from SQL to Oracle or vice versa). Pre-Requisites: 1. Java JDK 1.5 or higher. 2. Any Database. 3. Any IDE like Eclipse or NetBeans. Getting Started with Hibernate. Explanation with respect to Eclipse Indigo IDE using JDK 1.6 & Hibernate 3.3.2-GA. Download JDK1.6 from below link: http://www.oracle.com/technetwork/java/javase/downloads/index.html Download Eclipse Indigo from below link: http://www.eclipse.org/downloads/ Select Eclipse IDE for Java Developers. Link to download Hibernate: http://olex.openlogic.com/packages/hibernate/3.3.2.GA Download the Hibernate 3.3.2.GA Binary.zip file Role of Hibernate in Java Application:

Role of Hibernate in Java Application

How to install Hibernate Framework. 1. Unzip the downloaded Hibernate 3.3.2.GA binary.zip file. 2. Open Eclipse, create a new Java Project, and give the name for the project. 3. Right Click on created project and go to properties to set the Build path where we use the jar files of the Hibernate Framework. As below

4. Select Build Path on LHS pane & on RHS select Libraries tab.

5. Click add External Jar Files & browse for file Hibernate3.jar (Contains the Hibernate framework API) in the location (hibernate-distribution3.3.2.GA\hibernate3.jar)where you the unzipped Hibernate 3.3.2.GA binary bundle & add it. 6. Also you have to add the JDBC type IV driver (odbc14.jar) from the installation folder of DB. 7. Again click add External Jar Files & go hibernate-distribution3.3.2.GA\lib\required folder inside the unzipped bundle & select all jar files in the required folder. 8. After you add the jar files mentioned in point 5, 6 & 7 it should look as below.

9. Once you have added the entire jar files, your project uses Hibernate Framework API. 10. Click Ok & thats it done. Creating a Simple Hibernate Program:

1. Before you start coding your 1st Hibernate program you need to create a file
with name hibernate.cfg.xml (very imp). Or u can simply copy the file (hibernate.cfg.xml) from the unzipped bundle hibernate-distribution3.3.2.GA\project\tutorials\web\src\main\resources & paste it under SRC folder in the eclipse as below: 2. Now open the hibernate.cfg.xml file and specify the driver class, URL username & password of the Database & also the SQL Dialect as in shown fig. (Im using Oracle so its Oracle driver in the XML, make changes according to your DB driver).

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> <property name="connection.username">username</property> <property name="connection.password">password</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> Now, leave the Mapping Resources empty which is the last line of the hibernate.cgf.xml file which will be updated later as below: <mapping resource=" "/>

3. Now create a POJO file.


Package pack1.pack; public class Person { private int id; private String firstname; private String lastname; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname;

} public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }

4. Next step is to create a Mapping Resource for the POJO file created above.
Right click on SRC & create a new file with name Person.hbm.xml or simply copy the existing file in location hibernate-distribution3.3.2.GA\project\tutorials\web\src\main\resources\org\hibernate\tutorial\do main & make the changes in the Person.hbm.xml file as below & delete only the <set> tags: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="pack1.pack"> <class name="Person" table="PERSON"> <id name="id" column="PERSON_ID"> <generator class="native"/> </id> <property name="age"/> <property name="firstname"/> <property name="lastname"/> </class> </hibernate-mapping>

5. Next Step is to specify the above created resource mapping (Person.hbm.xml)


path in the hibernate.cfg.xml which was left blank while creating, file as below: <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> <property name="connection.username">username</property> <property name="connection.password">password</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> Now, leave the Mapping Resources empty which is the last line of the hibernate.cgf.xml file which will be updated later as below: <mapping resource=pack1/pack/Person.hbm.xml "/>

6. Last Step is to create the Client program:


package pack1.pack; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Test { public static void main(String[] args) { //Create a Person object & set the Values. Person p1 = new Person(); p1.setFirstname("AAA"); p1.setLastname("R"); p1.setAge(24); //Configure Hibernate using hibernate.cfg.sml contents Configuration c1 = new Configuration(); c1.configure(); // Important Step //To get Session objects from SessionFactory SessionFactory sf = c1.buildSessionFactory(); //Usage of session object Session s1 = sf.openSession(); // Select openSession(), not getCurrentSession()

//To Begin the transaction s1.beginTransaction(); s1.save(p1); s1.getTransaction().commit(); //To commit the Transaction //To close s1.flush(); s1.close();

} }

7. Run the program & check in the Database a Table with name as Person would
be created with the id 1 (auto-generated), firstname -- AAA, lastname R & age 24.

NOTE: YOU HAVE TO CREATE A POJO(PERSON.JAVA) & MAPPING FILE(PERSON.HBM.XML).

You might also like