JPA does not reflect the changed data in the BD manually

Asked

Viewed 84 times

4

I have a Javafx application with Hibernate (JPA).

In this application I update a Tableview with the data I search from BD (Mysql).

Every time I click for the system to search in the BD some data, it executes the following code:

public Task<List<Pedido>> getPedido(EntityManager em, String numeroPedido){
        return new Task<List<Pedido>>(){
            @Override
            protected List<Pedido> call() throws Exception {                
                TypedQuery<Pedido> query;
                query = em.createQuery("SELECT p FROM Pedido p WHERE p.numPedido = :arg1", Pedido.class);
                query.setParameter("arg1", numeroPedido);
                List<Pedido> lst = query.getResultList();

                return lst;
            }
        };
    }

This execution brings a list of requests whose order number is in the clause WHERE. So far, so good.

Keeping the application open, I go to the BD and manually change the data.

After that I run again the query that should bring the data I just changed, but, the data I receive is still the old.

If I close and open the application, then I get the changes I made manually in the BD.

The impression is that there is some kind of cache configured.

I have also tried to put the line below in my persistence.xml but was not successful.

<!-- Disable the second-level cache  -->
<property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />  

What may be occurring so that my application, even after executing the query, does not search the BD data I manually changed?

Complement to the answer:

Following the guidelines of colleague Joaquim, I created a class of connection with the comic. This class has a connection factory where I request a connection every time I access the BD.

public class Conexao {

    private static final Logger logger = LogManager.getLogger("Conexao");   
    private static EntityManagerFactory emf;

    static 
    {
        try {
            emf = Persistence.createEntityManagerFactory("crp");
        } catch (Exception e) {
            logger.error("Erro em Persistence.createEntityManagerFactory(crp) => " + e.getMessage() );
            e.printStackTrace();
        }
    }

    public EntityManager getEntityManager() throws Exception {
        //responsavel pela persistencia
        return emf.createEntityManager();
    }

    public void close() {
        emf.close();
    }    
}

When I need to call a reading function, I open the connection, read and close it.

public Task<List<Pedido>> getPedido(String numeroPedido){
        return new Task<List<Pedido>>(){
            @Override
            protected List<Pedido> call() throws Exception {
                EntityManager em = MainApp.getConexao().getEntityManager();             

                TypedQuery<Pedido> query;
                query = em.createQuery("SELECT p FROM Pedido p WHERE p.numPedido = :arg1", Pedido.class);
                query.setParameter("arg1", numeroPedido);
                List<Pedido> lst = query.getResultList();

                em.close();

                return lst;
            }
        };
    }

Thus, even if the BD is changed by some other application, whenever calling the function getPedido(), I’ll have the data updated.

1 answer

2


There is the native implementation of First Level Cache, see more here, a solution is you use the .clear() before consultations, in your case is it really necessary to change these directly at the base? Try to avoid this type of approach.

entityManager.clear();
  • Reading your answer, I found what was wrong. I actually created an Entitymanager for the entire application and used only it. From what I understand, I must instantiate a Entitymanager factory and for each request to the BD, request an Entitymanager. I will edit my question and put there the Entitymanager factory code. Thanks for the help...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.