hibernate Getting started with hibernate Simple hibernate example using XML


To set up a simple hibernate project using XML for the configurations you need 3 files, hibernate.cfg.xml, a POJO for each entity, and a EntityName.hbm.xml for each entity. Here is an example of each using MySQL:


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

   <property name="hibernate.dialect">
   <property name="hibernate.connection.driver_class">

   <property name="hibernate.connection.url">
   <property name="hibernate.connection.username">
   <property name="hibernate.connection.password">

   <!-- List of XML mapping files -->
   <mapping resource="HibernatePractice/Employee.hbm.xml"/>


DBSchemaName, testUserName, and testPassword would all be replaced. Make sure to use the full resource name if it is in a package.


package HibernatePractice;

public class Employee {
    private int id;
    private String firstName;
    private String middleName;
    private String lastName;
    public Employee(){
    public int getId(){
        return id;
    public void setId(int id){
        this.id = id;
    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;


   <class name="HibernatePractice.Employee" table="employee">
      <meta attribute="class-description">
         This class contains employee information. 
      <id name="id" type="int" column="empolyee_id">
         <generator class="native"/>
      <property name="firstName" column="first_name" type="string"/>
      <property name="middleName" column="middle_name" type="string"/>
      <property name="lastName" column="last_name" type="string"/>

Again, if the class is in a package use the full class name packageName.className.

After you have these three files you are ready to use hibernate in your project.