Steps
to Develop Hibernate Application
Using Net Beans
·
New à Other à Hibernate à Hibernate Configuration Wizard
·
New à Other à Hibernate à Hibernate Reverse Engineering Wizard
·
New à Other à Hibernate à Hibernate Mapping Files& POJOs
from Database
·
New à Other à Hibernate à Hibernate Utility class
·
Write Normal java class for Logic , which deals with
Configuration,SessionFactory,Session,Transaction.
Hibernate Tutorial with Examples
Our First
Application: Hib1_Insert
1.
hibernate.cfg.xml:
·
It contains the all the Details about Database
·
It is used to Connect our Application with Database
·
It contrails All the connections Properties for Connection
with the DATABASE, like
I.
hibernate.dialect : It
is for Recognizing the Database by HIBERNATE
II.
hibernate.connection.driver_class:Like JDBC Driver class
III.
hibernate.connection.url : like localhost:8080
IV.
hibernate.connection.username : database USERNAME
V.
hibernate.connection.password : Database Password
·
It also contains the property like ,if want to print the
query or not in output
<property
name="show_sql">true</property>
·
It also contains the “Hibernate Mapping File Information”
<mapping
resource="hib1_insert/Emp.hbm.xml"/>
hibernate.cfg.xml
<?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>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?zeroDateTimeBehavior=convertToNull</property>
<property
name="hibernate.connection.username">root</property>
<property
name="hibernate.connection.password">root</property>
<property
name="show_sql">true</property>
<mapping
resource="hib1_insert/Emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.
Hibernate POJO Class (Emp.java)
·
It is a class form of a Database TABLE
·
It contains all the Column names as Parameters
·
It has Setter() & Getter() methods for those column
names
·
It must implements java.io.Serializable
Emp.java
package
hib1_insert;
public
class Emp implements
java.io.Serializable {
private int sno;
private String name;
private Integer age;
public Emp() {
}
public Emp(int sno) {
this.sno = sno;
}
public Emp(int sno, String name, Integer
age) {
this.sno = sno;
this.name = name;
this.age = age;
}
public int getSno() {
return this.sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3.
Hibernate Mapping File (Emp.hbm.xml)
·
It is used to Map the Database Table with the POJO class
·
It contains <class> tag for mapping POJO class & Table
<class name="hib1_insert.Emp"
table="emp" catalog="hibernate">
·
<id> tag used to mapping PrimaryKey column with POJO
class variable
<id
name="sno" type="int">
<column name="sno" />
<generator class="assigned"
/>
</id>
·
<property> tag used to mapping normal column with POJO
class variables
<property
name="name" type="string">
<column name="name"
length="50" />
</property>
Emp.hbm.xml
<?xml
version="1.0"?>
<!DOCTYPE
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Generated Oct 19, 2013 10:54:49 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="hib1_insert.Emp"
table="emp" catalog="hibernate">
<id name="sno"
type="int">
<column name="sno"
/>
<generator
class="assigned" />
</id>
<property name="name"
type="string">
<column name="name"
length="50" />
</property>
<property name="age"
type="java.lang.Integer">
<column name="age"
/>
</property>
</class>
</hibernate-mapping>
4.
Client Application (Hib1_Insert.java)
·
It is a Normal Java class with main() method
·
It will contains the Execution Logic/ Business Logic for
perform CURD Operations
·
The Structure of Client application as Follows
·
Activate Hibernate Software
Configuration cfg=new Configuration()
·
Read the Hibernate configuration file
Hibernate
config file.cfg=cfg.configure(“\hibernate.cfg.xml”);
·
Create the Hibernate configuration Object
·
Create SessionFactory Object (It Represents the Connection
Polling object of JDBC)
SessionFactory factory = cfg.buildSessionFactory
·
Create Session Object
Session
res=factory.openSession()
·
Create Hibernate POJO class object for set/get values from
database
Emp ob = new Emp();
ob.setSno(12);
ob.setName("Twlwen");
ob.setAge(120);
·
Write code for CURD Operations
(CREATE,UPDATE,RETRIVE,DELETE)
Transaction tx = sess.beginTransaction();
sess.save(ob);
tx.commit();
5.
Run Client Application
6.
Output----Njoy