Saturday, September 25, 2010

Hibernate example to insert data into database

This is an simple hibernate example to insert records into database.In this i will be explaining you how to create a new project Eclipse IDE.
Steps for starting a new project in Eclipse IDE
1)File-->New-->Project

2) Select javaproject and click next
3) Click finish

4) Click No
5) Right click src folder and create package subin
6) Inside subin package create two java classes Insertion.java and InsertionDetails.java
Insertion.java
package subin;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class Insertion {

/**
* @subinsuresh
*/
public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
org.hibernate.Transaction tx = session.beginTransaction();

//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
InsertionDetails details = new InsertionDetails();
details.setId(1);
details.setFirstName("Subin");
details.setLastName("Suresh");
details.setAddress("xyz");
session.save(details);
System.out.println("Done");
tx.commit();
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
session.flush();
session.close();
}
}
}
InsertionDetails.java
package subin;

public class InsertionDetails {
private String firstName;
private String lastName;
private String address;
private long id;



/**
* @return First Name
*/
public String getFirstName() {
return firstName;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

/**
* @return Last name
*/
public String getLastName() {
return lastName;
}


/**
* @param string Sets the First Name
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param string sets the Last Name
*/
public void setLastName(String string) {
lastName = string;
}

/**
* @return ID Returns ID
*/
public long getId() {
return id;
}

/**
* @param l Sets the ID
*/
public void setId(long l) {
id = l;
}
}
7) Code for  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.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">web_test1</property>
<property name="hibernate.connection.password">web_test1</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="hibernate.hbm.xml" />
</session-factory>
</hibernate-configuration>
8) Code for  hibernate.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">
<hibernate-mapping>
<class name="subin.InsertionDetails" table="INSERTIONTABLE">
<id name="id" type="long" column="ID">
<generator class="assigned"/>
</id>
<property name="firstName">
<column name="FIRSTNAME"/>
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="address">
<column name="Address"/>
</property>
</class>
</hibernate-mapping>

Note: we dont have to create table manually it will be created  automatically in the database.In hibernate.hbm.xml we have specified class name and table name so it maps elements in InsertionDetails.java with column name in database. In this  example we have given
<id name="id" type="long" column="ID">
<generator class="assigned"/>
</id>
to make  id is the primary key.so we cannot insert more than one record with same id value
to prevent that we use class="increment".
9) We need to add necessary jar file otherwise it will give reference problem.To solve this right click project--> properties Add External Jar as shown in the figure below and click Ok.


9 ) Run the project as shown below


 10) You will get following result in console and one row will be inserted to INSERTIONTABLE.

No comments:

Post a Comment