Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package it.begear.dao; import it.begear.hibernate.HibernateUtil; import java.io.Serializable; import java.math.BigDecimal; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author Mario */ public class GenericDaoImpl<ID extends Serializable> implements GenericDao { protected Session getSession() { return HibernateUtil.getSession(); } @Override public void save(Object object) { Session hibernateSession = this.getSession(); Transaction tx = hibernateSession.beginTransaction(); hibernateSession.saveOrUpdate(object); tx.commit(); } @Override public void merge(Object object) { Session hibernateSession = this.getSession(); Transaction tx = hibernateSession.beginTransaction(); hibernateSession.merge(object); tx.commit(); } @Override public void delete(Object object) { Session hibernateSession = this.getSession(); Transaction tx = hibernateSession.beginTransaction(); hibernateSession.delete(object); tx.commit(); } @Override public List findMany(Query query) { List<Object> object; object = (List<Object>) query.list(); return object; } @Override public Object findOne(Query query) { Object object; object = (Object) query.uniqueResult(); return object; } @Override public List findAll(Class clazz) { Session hibernateSession = this.getSession(); Transaction tx = hibernateSession.beginTransaction(); List object; Query query = hibernateSession.createQuery("from " + clazz.getName()); object = query.list(); tx.commit(); return object; } @Override public Object findByID(Class clazz, BigDecimal id) { Session hibernateSession = this.getSession(); Transaction tx = hibernateSession.beginTransaction(); Object object; object = (Object) hibernateSession.get(clazz, id); tx.commit(); return object; } }