I have questions about how to work with objects EntityManagerFactory
and EntityManager
.
Currently I instantiate a EntityManagerFactory
for the whole system, as I only have one bank, I create only one and use it to create my EntityManager
. The EntityManager
I usually instantiate one per screen, or one for each crud. Is this certain way to manage them?
Here comes my second question :
The following is happening:
- User 1 searches the registry x
- User 2 makes a change to the registry x
- User 1 updates the screen and the registry x remains unchanged, as if User 2 had not interacted
- E in the database is updated as User 2 has changed
- If I close the entire system of User 1 and reopen there is the updated x-register
Why do not you upgrade to User 1? Will I always have to create a new EntityManager
for each database interaction?
Data search method:
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;
}
Excerpt 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();
}