Skip to main content

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.

Comments

Popular posts from this blog

The security log on this system is full. Only administrators can log on to fix the problem.

The security log on this system is full. Only administrators can log on to fix the problem. To resolve this issue, use an account that is a member of the Administrators group to log on to the computer. Then, follow these steps to specify that Security log events can be overwritten: 1. Click Start, point to All Programs, point to Administrative Tools, and then click Event Viewer. 2. Right-click Security, and then click Properties. 3. In the Log Size area of the Security Properties window, click the Overwrite events as needed option under When maximum log size is reached. 4. Click OK. 5. Close Event Viewer.

CWBLM0011 - An Internal License Management Error Occurred.

CWBLM0011 - An Internal License Management Error Occurred. RESA II started recieving error messages when users would sign on with client access express running on Windows 2000, the first client would connect but the second would get a license code error that said internal licensing error, here is a description from IBM's website, "Windows NT/2000 Restricted Users (User Group) attempting to start a 5250 terminal emulation session running Client Access Express (5769XE1) V5R1M0 receive a CWBLM0011 error message. This error does not occur when logged on to the PC as the Windows Administrator. The error appears more frequently when the users attempt to start multiple Client Access sessions." It appears to happen because the user doesn't have permissions to update some registry keys that client access likes to update occasionally, so this error message can happen at any time and doesn't appear to have any rhyme or reason. In RESA II's case, Tim believed it was ca...