具有休眠参数化的DAO通用

时间:2019-07-28 21:31:27

标签: java hibernate

到目前为止,我有这个:

我的DAO:

import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;

import util.HibernateUtil;

public abstract class AbstractDAO<T> {
    private Class classe;
    private Session session;

    public AbstractDAO(){
        this.classe = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    protected Session getSession(){
        if(this.session == null || !this.session.isOpen()){
            this.session = HibernateUtil.getSessionFactory().openSession();
        }
        return this.session;
    }

    public void save(T t){
        getSession().beginTransaction();
        getSession().save(t);
        getSession().close();
    }

    public void delete(T t){
        getSession().beginTransaction();
        getSession().delete(t);
        getSession().close();
    }

    public void alter(T t){
        getSession().beginTransaction();
        getSession().update(t);
        getSession().close();
    }

    public List<T> findAll(T t){
        return getSession().createCriteria(this.classe).list();
    }

         /**
     * Metodo responsavel por recuperar todos os objetos de uma tabela da base de dados de acordo
     * com o exemplo passado.
     *
     * @param filtro
     * @param matchMode
     * @param ignoreCase
     * @return lista
     */
    public List<T> findByExample(T filtro, MatchMode matchMode, boolean ignoreCase){
        org.hibernate.criterion.Example example = org.hibernate.criterion.Example.create(filtro);

        if(matchMode != null){
            example = example.enableLike(matchMode);
        }

        if(ignoreCase){
            example = example.ignoreCase();
        }

        return getSession().createCriteria(this.classe).add(example).list();
    }
}

我的休眠实用程序:

public class HibernateUtil {  

    private static final SessionFactory sessionFactory;  

    static {  
        try {  
            // Create the SessionFactory from hibernate.cfg.xml  
            sessionFactory = new Configuration().configure().buildSessionFactory();  
        } catch (Throwable ex) {  
            // Make sure you log the exception, as it might be swallowed  
            System.err.println("Initial SessionFactory creation failed." + ex);  
            throw new ExceptionInInitializerError(ex);  
        }  
    }  

    public static SessionFactory getSessionFactory() {  
        return sessionFactory;  
    }  

}  

主菜单:

public static void main(String[] args) {
    Departamento depart = new Departamento();
    AbstractDAO<Departamento> padrao = new DepartamentoDAO();
    padrao.findAll(depart);
    System.out.println(padrao);
}

我遇到此错误:

  

线程“ main”中的异常java.lang.ClassCastException:   无法将java.lang.Class强制转换为java.lang.reflect.ParameterizedType     在dao.AbstractDAO。(AbstractDAO.java:16)在   dao.DepartamentoDAO。(DepartamentoDAO.java:17)在   view.eeee.main(eeee.java:12)

不确定是否有人能帮助我调整代码,我是否做得最好

0 个答案:

没有答案