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 org.munie.envwater.dao; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; /** * * @author mnn */ @Transactional public class BaseDaoImpl implements BaseDao { @Autowired private SessionFactory factory; public void save(Object obj) { Session session = factory.getCurrentSession(); session.save(obj); } public void update(Object obj) { Session session = factory.getCurrentSession(); session.update(obj); } public void delete(Object obj) { Session session = factory.getCurrentSession(); session.delete(obj); } public Object get(Class clazz, int id) { Session session = factory.getCurrentSession(); return session.get(clazz, id); } public List getAll(Class clazz) { Session session = factory.getCurrentSession(); return session.createQuery("FROM " + clazz.getSimpleName()).list(); } public List query(String hql) { Session session = factory.getCurrentSession(); return session.createQuery(hql).list(); } public List querySql(String sql) { Session session = factory.getCurrentSession(); return session.createQuery(sql).list(); } }