Saturday, February 27, 2016


Core Classes and Interfaces

Hibernate API

ORM(Object Relation Mapping)

pojo: plain ordinary java object

A pojo object
  1. Has at least a primary key field
  2. other fields
  3. public get/set functions
  4. a non-parameter construct function(for hibernate reflection use)

Configuration :
Load hibernate.cfg.xml
Manage ORM files
Load driver, url, login
Manage hibernate configs

SessionFactory :
Get session instance from SessionFactory
Initialized when application starts
Occupied large memory, only need one instance for each database, so use singleton for each database
Cache sql statements and some data (session level cache)

Session : generated from sessionFactory

Transaction : transaction is required, so remember to commit


Employee pojo -> employee table


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
     <property name="connection.username">root</property>
     <property name="connection.password">root</property>
     <property name="connection.url">jdbc:mysql://</property>
     <property name="show_sql">true</property>
     <mapping resource="com/gvace/domain/Employee.hbm.xml"/>


hmb.xml file: manage relationship between a pojo class and a table

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<!-- Generated Feb 26, 2016 10:59:22 PM by Hibernate Tools 3.4.0.CR1 -->
    <class name="com.gvace.domain.Employee" table="employee">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="increment" />
        <property name="name" type="java.lang.String">
            <column name="NAME" not-null="true"/>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" not-null="true" />
        <property name="hiredate" type="java.util.Date" >
            <column name="HIREDATE" not-null="true"/>

pojo class, require serialize

package com.gvace.domain;

public class Employee implements Serializable{
 private static final long serialVersionUID = -3742562662542696434L;
 private Integer id;
 private String name;
 private String email;
 private java.util.Date hiredate;
 public Integer getId() {
  return id;
 public void setId(Integer id) { = id;
 public String getName() {
  return name;
 public void setName(String name) { = name;
 public String getEmail() {
  return email;
 public void setEmail(String email) { = email;
 public java.util.Date getHiredate() {
  return hiredate;
 public void setHiredate(java.util.Date hiredate) {
  this.hiredate = hiredate;
 public Employee() {

Example of using the pojo

 public void test(){
  //1. Initiate config, load config file
  Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
  //2. Create SessionFactory, normally use singleton
  ServiceRegistry serviceRegistry;
  serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
  SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  //3. Create session
  Session session = sessionFactory.openSession();
  //4. Start a transaction
  Transaction transaction = session.beginTransaction();
  //5. Add an employee
  Employee employee = new Employee();
  employee.setHiredate(new Date());
  //6. Save;
  //7. Commit
  //8. Close session

No comments:

Post a Comment