Hibernate : Simple Data Entry

After studying about Hibernate Architecture, what are the next steps?

This post, we will tackle only Hibernate Best Practices and we'll not go into deeper explanation about it.



1. First, you must have a database. Hibernate supported many databases. But not in SQL Server. I don't know but, I'm having errors when i used SQL Server specially SQL Server 2000. In my case, i prefer to use mySQL.

2. Create a table, try the simple on. It may contain name of the Members of your family, friends or even Officemates. It depends on you. But make it simple as possible.

/*!40101 SET NAMES utf8 */;

create table `tbl_employees` (
`PersonalID` double ,
`FirstName` varchar (75),
`MiddleName` varchar (75),
`LastName` varchar (75)
);

3. Download Hiberate3, dom4j, commons-logging, commons-collections, mysql-connector-java, c3pO, jta, log4j jar files.

3. Make your Java Project.

4. After the Step #3 is finished, create a folder from your java project. Name it "lib". Copy-Paste the jar files to the lib folder then configure your build path and include them.

5. Create a hibernate configuration file. Then paste this code.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/hibernatetutorial</property>
<property name="hibernate.connection.username">[DATABASE_USERNAME]</property>
<property name="hibernate.connection.password">[DATABASE_PASSWORD]</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="[MAPPING_FILE]"/>
</session-factory>
</hibernate-configuration>

Note: Make sure that you create the configuration file inside src folder and you state the exact location of the MAPPING_FILE (e.g. com/hibernate/testing/mapping.hbm.xml)

6. Next, create a POJO(Plain Old Java Objects) or let simply say a Class with a setter and getter.

public class Employees {
private String firstName;
private String middleName;
private String lastName;

public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

Note: Whatever the columns existing in your table, it must exist in your class object.

7. Lets create a mapping file.

<?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 package="com.employees.bry">
<class name="Employees" table="TBL_EMPLOYEES">
<id>
</id>
<property name="firstName">
<column name="FirstName"></column>
</property>
<property name="middleName">
<column name="MiddleName"></column>
</property>
<property name="lastName">
<column name="LastName"></column>
</property>
</class>
</hibernate-mapping>

Note : Like the configuration file, the mapping file must be with the configuration file inside the src folder.

8. Create a Client class. Name it EmployeeClient.java then paste this code.

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

public class EmployeeClient {

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

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
session.beginTransaction();
System.out.println("Inserting Record");
Employees employees = new Employees();
employees.setFirstName("This is a first name");
employees.setMiddleName("This is a middle name");
employees.setLastName("This is a last name");
session.saveOrUpdate(employees);
System.out.println("Done");
session.getTransaction().commit();
session.close();
}
}

The only thing you must remember is the configuration file, mapping file, and the POJO. If you are new to hibernate, make this post as your reference. Try to read more about hibernate then try to retrieve the data that we have entered in this post.

No comments: