在我们的开发团队中,我注意到了两种使用 EntityManager 的方法:
1. 第一种方式
public class ReferentielDaoImpl implements ReferentielDaoLocal {
@PersistenceContext(unitName = "ErpCCF-ejbPU")
private EntityManager em;
public List<Alerte> findAll(){
try {
em.getEntityManagerFactory().getCache().evictAll();
String req = "SELECT a FROM Alerte a ORDER BY a.idAlerte DESC";
List<Alerte> alertes = em.createQuery(req).getResultList();
return alertes;
} catch (Exception e) {
e.printStackTrace(System.out);
}
return null;
}
}
2.第二种方式
public class ReferentielDaoImpl implements ReferentielDaoLocal {
@PersistenceContext(unitName = "ErpCCF-ejbPU")
private EntityManager em;
public List<Alerte> findAll(){
try {
String req = "SELECT a FROM Alerte a ORDER BY a.idAlerte DESC";
List<Alerte> alertes = em.createQuery(req).getResultList();
return alertes;
} catch (Exception e) {
e.printStackTrace(System.out);
}
return null;
}
}
这两种方法有什么区别?
正确的方法是什么?