Using Hibernate to insert an entry into database
This example will show how to use hibernate to store value into database.
Dependencies:
Download the hibernate 3.2 jar and hibernate-annotations.jar from hibernate.org.
Download the entire relase. The release will also contain other dependent jars.
Please include all this jars into the classpath.
Description:
1. Database
To write a hibernate application we need to setup a database.
For our example we are using MYSQL database which is freeware.
Create a table employee with fields (id, name, address) in database.
Name the schema as tutorial.
2. Configuration file
we need to write hibernate.cfg.xml to configure hibernate.
This configuration file should be in the class path.
Hibernate.cfg.xml (classpath)
<?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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/tutorials</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping class="Employee"/>
</session-factory>
</hibernate-configuration>
3. Entity class
Employee.java
We need to write a simple pojo class for each table in database. For our employee table in database we will write a pojo/entity class Employee.java (Note: the name of pojo class and the name of table in database need not be same.)
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
private long id;
@Column(name="name")
private String name;
@Column(name="address")
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now @Entity denotes that the class Employee is a representation of database entity/table.
@Id says that the id column in table is primary key.
@Column says that the attribute following it is one of the column in table. (by default attribute is mapped by name. i.e if don’t specify the name attribute in Column annotation, by default name will be same as the property name.)
4. Accessing database using hibernate
HibernateApplication.java (Main class)
From our main class we will insert a employee record in database.
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateApplication {
private static SessionFactory sessionFactory=null;
static{
sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
}
public static void main(String[] args) {
System.out.println("Hello");
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
Employee emp=new Employee();
emp.setName("Sourabh");
emp.setAddress("India");
emp.setId(1);
session.save(emp);
transaction.commit();
session.flush();
session.close();
}
}
To know more about SessionFactory, Session, Transaction please view the Hibernate Tutorial #1 – Introduction here or hibernate.org website.
This is all required to run a hibernate application.
This is a simple application showing how to create a record in database.
Short recap of files used
hereEmployee.java
HibernateApplication.java
Hibernate.cfg.xml
Hibernate3.jar
Hibernate-annotations.jar
Hibernate-commons-annotations.jar
Commons-collection.jar
Commons-logging.jar
Dom4j.jar
Jta.jar
Cglib.jar
Mysql-connector-java-3.jar
Asm.jar