This tutorial demonstrates how to use Hibernate which is an Object relational mapping tool for Java platform.
This is a simple example of creating an object in Java and
mapping it to a table in mysql database. (inserting rows into the mysql
database using Hibernate)
Before we start with the application we need the following:
- Tomcat 5.0 or above. Click here to download
- Eclipse IDE ( Enterprise Edition).
- Hibernate x.x jar files. For the latest version of jar files Click here to download
- MySQL database
- MySQL ODBC driver jar file. Click here to download
Step 1: Create a Java project in Eclipse.
Step 2: Provide a name for your project and click on ‘Finish’
Step 3: Add the following jars (which are present in the downloaded hibernate x.x -> lib -> required folder) and also the downloaded mysql odbc jar file to the project by right clicking on the project name followed by Properties ->Java Build Path -> Libraries -> Add External jars
Step 4: Create the hibernate.cfg.xml file in the src folder
of the project and copy the following code into the xml file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="dialect">org.hibernate.dialect.MySQLDialect</property> | |
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> | |
<property name="connection.url">jdbc:mysql://localhost:port/database_name</property> | |
<property name="connection.username">root</property> | |
<property name="connection.password">password</property> | |
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property> | |
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> | |
<property name="current_session_context_class">thread</property> | |
<property name="hibernate.show_sql">true</property> | |
<property name="hibernate.hbm2ddl.auto">create</property> | |
<mapping class="com.model.Employee"/> | |
</session-factory> | |
</hibernate-configuration> |
Step 5: Update the hibernate.cfg.xml file parameters ( port number, database name, username password). The final hibernate.cfg.xml should look as follows:
Step 6: Create a package ‘com.model’ and create two classes named ‘Employee’ and ‘HibernateMain’ within that package and paste the following codes in the respective class files:
Employee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.model; | |
import java.util.Date; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
@Entity | |
@Table (name="employee") | |
public class Employee { | |
@Id | |
private int eid; | |
private String ename; | |
private String eadd; | |
private Date doj; | |
public int getEid() { | |
return eid; | |
} | |
public void setEid(int eid) { | |
this.eid = eid; | |
} | |
public String getEname() { | |
return ename; | |
} | |
public void setEname(String ename) { | |
this.ename = ename; | |
} | |
public String getEadd() { | |
return eadd; | |
} | |
public void setEadd(String eadd) { | |
this.eadd = eadd; | |
} | |
public Date getDoj() { | |
return doj; | |
} | |
public void setDoj(Date doj) { | |
this.doj = doj; | |
} | |
} |
Hibernate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.model; | |
import java.util.Date; | |
import org.hibernate.Session; | |
import org.hibernate.SessionFactory; | |
import org.hibernate.cfg.Configuration; | |
public class HibernateMain { | |
public static void main(String[] args) { | |
Configuration configuration=new Configuration(); | |
SessionFactory sf=configuration.configure().buildSessionFactory(); | |
Employee e1=new Employee(); | |
e1.setEid(1); | |
e1.setEname("Sachin"); | |
e1.setEadd("India"); | |
e1.setDoj(new Date()); | |
Employee e2=new Employee(); | |
e2.setEid(2); | |
e2.setEname("Rooney"); | |
e2.setEadd("Manchester"); | |
e2.setDoj(new Date()); | |
Session ss=sf.openSession(); | |
ss.beginTransaction(); | |
//saving objects to session | |
ss.save(e1); | |
ss.save(e2); | |
ss.getTransaction().commit(); | |
ss.close(); | |
} | |
} |
Step 7 : Your final directory structure should look as
follows:
Step 8: Run the HibernateMain.java as a Java
Application. The console should show your insert
queries as follows:
If you open mysql and check the database, the two employee rows
should be present.
Notes:
- The Annotations in the Employee.java file help Hibernate decide the structure of the table to be created in the database. For example, @Entity tells Hibernate to treat this object as a table in the database. @Id annotation just above the declaration of parameter eid in the Employee.java file tells Hibernate to treat eid as the primary key of the table. @Table (name="employee") decides the name of the table which will be created in the database.
- For every new model class for which we need to create a table we need to give the @Entity annotation in the class file itself and add the mapping parameter in the hibernate.cfg.xml file as <mapping class="com.model.class_name"/>
- The <property name="hibernate.hbm2ddl.auto">create</property>
which is set to create in our example automatically creates a schema destroying the previous data
The list of possible values for this parameter are,
- validate: validate the schema, makes no changes to the database.
- update: update the schema.
- create: creates the schema, destroying previous data.
- create-drop: drop the schema at the end of the session.