This project provides the hibernate basics for XML and Annotation Mapping.

Follow the class lecture

  • Use our User Library HibernateLibs

Code

The code below shows portions of the code needed for Hibernate XML configuration, XML mapping, and Annotations mapping.

Application AppMainStart.java

package com.hibernateclass;

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

public class AppMainStart {

	public static void main(String[] args) {
				
		System.out.println("Start Hibernate");
		SessionFactory sf = new Configuration().configure().buildSessionFactory();		
		Session session = sf.openSession();
		
		// Session 1
		System.out.println("start session 1");
		session.beginTransaction();

		// code here
		
		session.getTransaction().commit();
		session.close();
		

		//Session 2
		System.out.println("start session 2");
		Session session2 = sf.openSession();
		session2.beginTransaction();

		// code here
		
		session2.getTransaction().commit();
		session2.close();
		System.out.println("End Hibernate");
	}

}


Model

Person Class (XML Mapping)

Not showing getters/setters, toString, hashCode, and equals.

package com.hibernateclass;

import java.util.Date;

public class Person implements Cloneable{
	private Long id;
	private String firstName;
	private String lastName;
	private int age;
	private String gender;
	private Date dob;
	private String email;

//....
}

Address Class (XML Mapping)

Not showing getters/setters, toString, hashCode, and equals.

package com.hibernateclass;

public class Address {
	private Long id;
	private String street;
	private String city;
	private String zip;
	private String state;

//...
}

Person Class (Annotation Mapping)

Not showing getters/setters, toString, hashCode, and equals.

package com.hibernateclass;

import java.util.Date;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;

@Entity
public class Person implements Cloneable{
	@Id
	@GeneratedValue
	private Long id;
	private String firstName;
	private String lastName;
	private int age;
	private String gender;
	private Date dob;
	@Transient
	private String email;

//....
}

Address Class (Annotation Mapping)

Not showing getters/setters, toString, hashCode, and equals.

package com.hibernateclass;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Address {
	private Long id;
	private String street;
	private String city;
	private String zip;
	private String state;

//...
}

Hibernate Configuration XML

hibernate.cfg.xml

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

<hibernate-configuration>
    
    <session-factory>

        <!-- Database configuration properties  -->        
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/people</property>
        <property name="connection.username">root</property>
        <property name="connection.password">password</property>
        <property name="connection.pool_size">1</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        
        <!-- Mapping files  -->
        <mapping resource="com/hibernateclass/Person.hbm.xml"/>
        <mapping resource="com/hibernateclass/Address.hbm.xml"/>
        <!-- mapping class="com.hibernateclass.Person"/ -->
        <!-- mapping class="com.hibernateclass.Address"/ -->           
    </session-factory>
    
</hibernate-configuration>




Hibernate Mapping XML

Person.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    
    <class name="com.hibernateclass.Person" table="person">
        
        <id name="id" type="java.lang.Long">
            <column name="id" />
            <generator class="identity" />
        </id>
        
        <property name="firstName" />
        
        <property name="lastName" type="string">
            <column name="Last_Name" />
        </property>
        
        <property name="gender" type="string">
            <column name="gender" />
        </property>
        
        <property name="age" type="int">
            <column name="age" />
        </property>
        
        <property name="dob" type="java.util.Date">
            <column name="Birthday" />
        </property>
    
    </class>
    
</hibernate-mapping>

Address.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    
    <class name="com.hibernateclass.Address" table="address">
        
        <id name="id" type="java.lang.Long">
            <column name="id" />
            <generator class="identity" />
        </id>
        
        <property name="street" />
        
        <property name="city" type="string">
            <column name="city" />
        </property>
        
        <property name="zip" type="string">
            <column name="zip_code" />
        </property>
        
        <property name="state" type="string">
        </property>
            
    </class>
    
</hibernate-mapping>


Database

CREATE DATABASE  IF NOT EXISTS `people`;

Importing the Project

Eclipse™ Java Project
Ex-C-HibernateStarterProject.zip
(Download to your downloads folder or another easily remembered folder.)

  1. Start Eclipse with a Workspace of
    c:javarunhibernate
  2. In Eclipse click the following:
    • File
    • Import
    • General
    • Existing Project into Workspace
    • Next
    • Select archive file:
    • Browse
    • go to zip location
    • Open
    • Choose project
    • Finish
  3. Have fun running the app.