Hi
I'm very new to hibernate, but has this is a very specific problem, i feel the need to make the question here.
I'm using Hibernate Core + Annotations + EntityManager in a Java EE environment, but without a EJB3 containner (i'm using tomcat).
I don't know if i'm using a correct arquitecture or not... it's just working for now!
My problem is that when i change "manually" the data in the database, the EntityManager does not load's this data again... the state of the Entity stays as if it was not updated.
So, to go around that problem, i'm calling the refresh() method of the entity manager for every entity read from the database, but although this is not an application with strong performance issues, i don't think this is the most elegant way of doing things...
Some code to show what i'm doing:
DbConn class: used to get the entity manager
Code:
public class DbConn {
private static EntityManager entityManager;
/**
* This method will return an instance of entityManager; If it has not yet been instancieted, it will create one;
* @return
*/
public static synchronized EntityManager getSrocEntityManager(){
if(entityManager==null){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sroc");
entityManager = emf.createEntityManager();
}
return entityManager;
}
}
A simple method to list Cliestes entity from the database:
Code:
public List<ClientesForm> findAll(){
//return em.createQuery("SELECT c FROM Clientes c").getResultList();
List<ClientesForm> result = new ArrayList();
List<Clientes> clientes = em.createQuery("SELECT c FROM Clientes c").getResultList();
Iterator it = clientes.iterator();
Clientes cl = null;
ClientesForm clForm = null;
while(it.hasNext()){
cl = (Clientes)it.next();
em.refresh(cl); //synchronize with the database
clForm = new ClientesForm();
clForm.setClienteId(cl.getClienteId());
clForm.setCodigoPostal(clForm.getCodigoPostal());
clForm.setEmail(cl.getEmail());
clForm.setFax(cl.getFax());
clForm.setMorada(cl.getMorada());
clForm.setNif(cl.getNif());
clForm.setNome(cl.getNome());
clForm.setSigla(cl.getSigla());
clForm.setTelefone(cl.getTelefone());
result.add(clForm);
}
return result;
}
Is there any way of make this work without having to call refreash for every object returned? Is this the correct approach?