8
I doubt how I should work with the objects EntityManagerFactory
and EntityManager
.
Currently I urge a EntityManagerFactory
for the whole system, since I have only one bank, I create only one and use it to create my own EntityManager
. The EntityManager
i usually instate one per screen, ie one for each crud. It is right this way to manage them?
Here comes my second question:
Here’s what’s going on:
Why not update to User 1? I’ll always have to create a new one EntityManager
for each interaction with the database?
Method that searches the data:
public List<Evento> getListPeriodo(Date inicio, Date fim, String texto) {
Query query = getDbManager().createNamedQuery("Evento.getListPeriodo");
query.setParameter("inicio", inicio);
query.setParameter("fim", fim);
query.setParameter("texto", "%" + texto + "%");
List<Evento> lista = query.getResultList();
return lista;
}
Section where I populate a Jtable with the search result:
EntityManager dbManager = Constante.DBFACTORYLOCAL.createEntityManager();
dbManager.clear();
EventoDAO EventoDAO = new EventoDAO(dbManager);
try {
List<Evento> oe = EventoDAO.getListPeriodo(di, de, jTextFieldFiltro.getText());
for (Evento t : oe) {
String cntr = "";
for (Item o : t.getItens()) {
if (cntr.isEmpty()) {
cntr = o.getPalavra();
} else {
cntr = cntr + ", " + o.getPalavra();
}
}
Object[] linha = new Object[]{t.getEventoId(), t.getDescricao(), cntr, DateFunction.DateUtilToTexto(t.getDataInicio())};
modelo.addRow(linha);
}
} finally {
dbManager.close();
}
Possible duplicate (or not): http://answall.com/questions/45758/bestforma-de-utilizar-e-instancer-o-entitymanagerfactory
– Victor Stafusa
What exactly are your screens? Swing components? JSP pages? Servlets? HTML forms with ajax? A lot of
System.out.println
?– Victor Stafusa