2014-01-27 138 views
0

我的代碼工作,我只是想知道,如果它寫得很好,我應該修改。 我使用不同的方法來執行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"); 
    } 
} 

是在我打開,關閉的方式,開始並提交事務對?或者我應該例如準時開啓和關閉會議?最佳做法是什麼?

回答

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

這只是要求sql注入。使用.setParameter來避免這種情況。

+0

是正確的,我只是忘了編輯。關於其他的東西? – Accollativo