Hibernate Tutorial

Hibernate CURD XML Based Example

Hibernate CURD:

Hibernate CURD operation using Save, Update, Delete, Get, CreateCriteria
1. SessionFactory
SessionDao.java
package com.candidjava.hibernate.curd.dao;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class SessionDao {
 
  private static SessionFactory factory;
  private Session session = null;
  static {
 
    factory = new Configuration().configure("hibernate.cfg.xml")
        .buildSessionFactory();
  }
 
  public Session getSession() {
    if (session == null) {
      session = factory.openSession();
    }
    return session;
  }
 
}
2. Hibernate Mapping
User.hbm.xml
<?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>
  <class name="User" table="Users">
    <id name="id" type="long" column="ID">
      <generator class="increment" />
    </id>
    <property name="userName" column="UName" not-null="true"
      unique="true" />
    <property name="password" column="Pwd" not-null="true" />
 
  </class>
</hibernate-mapping>
3. POJO Class
User.java
package com.candidjava.hibernate.curd.bean;
 
public class User {
 
  private long id;
  private String username;
  private String password;
 
  public long getId() {
    return id;
  }
 
  public void setId(long id) {
    this.id = id;
  }
 
  public String getUsername() {
    return username;
  }
 
  public void setUsername(String username) {
    this.username = username;
  }
 
  public String getPassword() {
    return password;
  }
 
  public void setPassword(String password) {
    this.password = password;
  }
 
}
4. UserDao.java
Session.save(Object ob)
Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.) This operation cascades to associated instances if the association is mapped with cascade=”save-update”.
public void insert(User u) {
    SessionDao sd = new SessionDao();
    session = sd.getSession();
    Transaction tx = session.beginTransaction();
    session.save(u);
    tx.commit();
    session.close();
  }
Session.get(Object.class, Long id)
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. (If the instance is already associated with the session, return that instance. This method never returns an uninitialized instance.)
public User getUser(Long id) {
    SessionDao sd = new SessionDao();
    session = sd.getSession();
    User u = (User) session.get(User.class, id);
    session.close();
    return u;
  }
Session.update(Object ob)
Update the persistent instance with the identifier of the given detached instance. If there is a persistent instance with the same identifier, an exception is thrown. This operation cascades to associated instances if the association is mapped with cascade=”save-update”.
public void updateUser(User u) {
    SessionDao sd = new SessionDao();
    session = sd.getSession();
    User user = (User) session.load(User.class, u.getId());
    user.setPassword(u.getPassword());
    user.setUsername(u.getUsername());
    Transaction tx = session.beginTransaction();
    session.update(user);
    tx.commit();
    session.close();
  }
Session.createCriteria(Object.class)
Create a new Criteria instance, for the given entity class, or a superclass of an entity class.
public List<User> getUserList() {
    SessionDao sd = new SessionDao();
    session = sd.getSession();
    List<User> li = session.createCriteria(User.class).list();
    session.close();
    return li;
  }
Session.delete(Object ob)
Remove a persistent instance from the datastore. The object argument may be an instance associated with the receiving Session or a transient instance with an identifier associated with existing persistent state. This operation cascades to associated instances if the association is mapped with cascade=”delete”.
public void deleteUser(Long id) {
    SessionDao sd = new SessionDao();
    session = sd.getSession();
    User u = (User) session.load(User.class, id);
    Transaction tx = session.beginTransaction();
    session.delete(u);
    tx.commit();
    session.close();
  }
Download Example