在hibernate中处理会话的正确方法

时间:2014-01-27 15:09:00

标签: java hibernate session hibernate-session

我的代码有效,我只是想知道它是否写得好,我应该修改什么。 我使用不同的方法来做MySQL选择,使用hibernate HQL和Criteria。主要是学习使用它们。 另一个问题是关于Hibernate会话,我不知道我是否以正确的方式使用它。

我有这个会话创建课程:

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {               
            //VER HIB 4
            Configuration configuration = new Configuration().configure();
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
            applySettings(configuration.getProperties());

            SessionFactory sf = configuration.buildSessionFactory(builder.build());

            return sf;          
        }
        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;
    }

}

我创建了一个DAO类:

public class CreazioneUtenzaDAO extends CommonDAO
{

    public CreazioneUtenzaDAO() {

    }

    public List<Comune> getComuni()
    {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Comune> com = session.createQuery("FROM Comune").list(); 
        session.getTransaction().commit();

        return com;
    }

    public List<Ente> getEntiFromComune(Comune comune) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        Criteria cri = session.createCriteria(Ente.class, "ente");
        //join on ente.tipoufficios as tipouff
        cri.createAlias("ente.tipoufficios", "tipouff");
        cri.createAlias("tipouff.ufficios", "uff");
        cri.add(Restrictions.eq("uff.comune", comune));
        cri.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);        
        List <Ente> en = cri.list();
        session.close();

        return en;
    }

    public List<Tipoufficio> getTipoufficioFromComuneEnte(String idComune, String idEnte) 
    {
        String strSql ="SELECT DISTINCT tu "+
                "FROM Tipoufficio tu INNER JOIN tu.ufficios as u "+ 
                "WHERE u.comune.codComune= :idComune AND tu.ente.idEnte= :idEnte "
                +"order by tu.descrizione";

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query query = session.createQuery(strSql);
        query.setParameter("idComune", idComune);
        query.setParameter("idEnte", idEnte);

        List<Tipoufficio> list = query.list();
        session.close();
        return list;
    }

    public List<Ufficio> getUfficiFromComuneEnteTipoufficio(String idComune,
            String idEnte, String codTipoUfficio) {
        String strSql ="SELECT DISTINCT u  "+
                "FROM Ufficio as u INNER JOIN u.tipoufficio as tu "+ 
                "WHERE u.comune.codComune= :idComune AND tu.ente.idEnte= :idEnte AND tu.idTipoUfficio= :codTipoUfficio "
                +" order by u.descrizione ";

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query query = session.createQuery(strSql);
        query.setParameter("idComune", idComune);
        query.setParameter("idEnte", idEnte);
        query.setParameter("codTipoUfficio", codTipoUfficio);
        List<Ufficio> list = query.list();
        session.close();
        return list;
    }

    public List<Email> getEmails() 
    {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Email> tmp = session.createQuery("FROM Email").list();         
        session.getTransaction().commit();
        return tmp;
    }

    public List<Applicazione> getApplicazioni() 
    {
        return selectAll("Applicazione");
    }

    public List<Qualifica> getQualifiche() {
        return selectAll("Qualifica");
    }
}

我打开,关闭,开始和提交交易的方式是正确的吗?或者我应该按时打开和关闭会话?哪种是最佳做法?

2 个答案:

答案 0 :(得分:0)

 u.comune.codComune='"+idComune+"'

这只是要求sql注入。使用.setParameter来避免这种情况。

答案 1 :(得分:0)