Hibernate Example on Eclipse and Oracle
Hibernate Tutorial explains you the details of this technology. Here is an a simple example which will help you to start your first hibernate web project on Eclipse and Oracle as database. I am not taking those free databases as example considering frequency of usage of Oracle. Below are the easy steps to be followed to make it all work. Futher you can take this example ahead with detail tutorial, or use it as your research ground to try other things in hibernate.
Create a Dynamic Web Project:
Follow the steps specified in similar tutorial Spring MVC Tutorial with Eclipse and Tomcat, just that the project name would be different. I used ‘HibernateExample’. This project will have complete folder structure created on your machine. To make it work with less effort, I added the resources forlder to the class path as shown below.

Add Libraries:
You will need following libraries to make it all work. Add those to ‘..\WebContent\WEB-INF\lib’ directory.
antlr-2.7.6.jar
asm-1.5.3.jar
asm-attrs-1.5.3.jar
cglib-2.1_3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate-3.2.5.ga.jar
jta.jar
ojdbc14.jar
Sample Code:
Below are the sample Java files, include those in /src/main/java directory. I am keeping the package default, change it as per your need. Just ensure you change all references of package, including those in xmls.
The domain object/bean class
Customer.java
import java.io.Serializable;
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
private Long customerId = null;
private String customerName;
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String aCustomerName) {
this.customerName = aCustomerName;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public boolean equals(Object candidate) {
if (candidate == this) {
return true;
}
if (candidate instanceof Customer) {
if ((this.getCustomerName() != null &&
((Customer)candidate).getCustomerName() != null)) {
return (this.getCustomerId()==
(((Customer)candidate).getCustomerId())&&
this.getCustomerName().equals(
((Customer)candidate).getCustomerName()));
}
}
return false;
}
public int hashCode() {
return this.getCustomerId().intValue();
}
}
Next is the class that interacts with database using hibernate and the domain object above.
CustomerDAO.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class CustomerDAO {
private Session session = null;
public void createSession(){
Configuration cfg = new Configuration()
.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
this.session = sf.getCurrentSession();
}
public Customer create(Customer newCustomer) {
Transaction transaction = session.beginTransaction();
session.save(newCustomer);
transaction.commit();
return newCustomer;
}
public static void main(String[] args){
Customer customer = new Customer();
customer.setCustomerName("Hibernate Example Customer");
System.out.println("Customer id before creation:" +
customer.getCustomerId());
CustomerDAO customerDAO = new CustomerDAO();
customerDAO.createSession();
Customer persistedCustomer = customerDAO.create(customer);
System.out.println("Customer id after creation:" +
persistedCustomer.getCustomerId());
}
}
This is how the Customer object hibernate mapping will be.
Customer.hbm.xml
TAB_CUSTOMER_SEQ
Next is the important configuration file. Change the database connection properties to match your Oracle database details.
hibernate.cfg.xml
org.hibernate.dialect.Oracle9Dialect oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@server:port:schema user password defaultschema false true false false org.hibernate.cache.NoCacheProvider thread
In all this is how the folder structure will look like.

Last is the table creation in database. Use these scripts to create simple table in database and also the sequence related to it.
CREATE TABLE TAB_CUSTOMER ( CUSTOMER_ID NUMBER(10) NOT NULL, CUSTOMER_NAME VARCHAR2(255) )
CREATE SEQUENCE TAB_CUSTOMER_SEQ START WITH 1 MAXVALUE 999999999999999999999999999 MINVALUE 1 NOCYCLE CACHE 20 NOORDER;
Run the CustomerDAO (main method) and it will work to insert the customer in database.
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Hi,
Nice tutorial. This will be very helpful for people who wants to learn Hibernate.
Thanks Viral.
This page which has simple example of Hibernate with Oracle is really helpfull for beginners to Hibernate.
Thanks.
I want to know where the jar files are located.
to try out this example
antlr-2.7.6.jar
asm-1.5.3.jar
asm-attrs-1.5.3.jar
cglib-2.1_3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate-3.2.5.ga.jar
jta.jar
ojdbc14.jar
You can download these versions from Maven Repository site – http://repo1.maven.org/maven2/.