Hibernate Tutorial

Hibernate Annotation CRUD Example

Hibernate Annotation CRUD operation using Save, Update, Delete, Get, CreateCriteria.
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;
  }
 
}

 User.java (POJO class):

package com.candidjava.hibernate.curd.bean;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name = "login")
public class User {
  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;
  @Column(name = "username")
  private String username;
  @Column(name = "password")
  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;
  }
 
}
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