Easy Data Access Object (DAO) with Hibernate

Before you start I suggest you read Installing PostgreSQL and setting up Hibernate

Create entities Robot and AI

Create a package called entity and inside create classes

Robot

package entity;

import javax.persistence.*;

@Entity
@Table(name = "robots")
public class Robot {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    @ManyToOne
    @JoinColumn(name = "ai")
    private AI ai;

    // getters and setters
}

 

AI

package entity;

import javax.persistence.*;

@Entity
@Table(name = "ai")
public class AI {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "version")
    private double version;

    // getters and setters
}

 

Creating the DAO

Create a packaged called dao and inside create classes:

Dao, this is the base class all daos will extend

package dao;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import utility.HibernateUtility;

import java.util.List;

public abstract class Dao<ENTITY> {

 private Class<ENTITY> clazz;

 public Dao(Class<ENTITY> clazz) {
 this.clazz = clazz;
 }
 
 /**
 * Opens session
 * @return
 */
 public Session openSession() {
 return HibernateUtility.getSessionFactory().openSession();
 }
 
 
 /**
 * Saves when entity does not exist and updates when it does
 *
 * @param entity
 */
 public void persist(ENTITY entity) {
 Session currentSession = openSession();
 currentSession.saveOrUpdate(entity);
 currentSession.close();
 }

 /**
 * Finds entity by primary key
 *
 * @param id
 * @return
 */
 public ENTITY findById(int id) {
 Session currentSession = openSession();
 ENTITY entity = currentSession.get(clazz, id);
 currentSession.close();
 return entity;
 }
 
 /**
 * Deletes an entity
 *
 * @param entity
 */
 public void delete(ENTITY entity) {
 Session currentSession = openSession();
 currentSession.delete(entity);
 currentSession.close();
 }
 
 /**
 * Finds all entities
 *
 * @return
 */
 public List<ENTITY> findAll() {
 Session currentSession = openSession();
 List<ENTITY> list = currentSession.createCriteria(clazz).list();
 currentSession.close();
 return list;
 }
 
 /**
 * Finds entity by given key and value
 *
 * @param key - field from entity class
 * @param value
 * @return
 */
 public List<ENTITY> findByKey(String key, Object value) {
 Session currentSession = openSession();
 List list = currentSession.createCriteria(clazz).add(Restrictions.eq(key, value)).list();
 currentSession.close();
 return list;
 }
}



Example

Create a class RobotDAO

package dao;

import entity.Robot;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import java.util.List;

// extends Dao with parameter the entity
public class RobotDao extends Dao<Robot> {

    public RobotDao() {
// call the constructor from Dao and pass the class of the entity
        super(Robot.class);
    }

    /**
     * List of all robots with this name
     * @param name
     * @return
     */
    public List<Robot> findByName(String name) {
        Session currentSession = openSession();
        List list = currentSession.createCriteria(Robot.class).add(Restrictions.eq("name", name)).list();
        currentSession.close();
        return list;
    }
}

 

Now simply

// initialize the dao
RobotDao robotDao = new RobotDao();
// list of 'robot' with name boty will be returned
robotDao.findByName("boty");

 

If you have any questions or suggestions, feel free to leave a comment.
Become a Patron!