Problem memory objects overwritten when consulting database

0

Good morning,

I have a problem that has been bothering me for two days. The problem in question, is that I have an X object with some data in memory, and when I consult in bbdd the X object to check if what I have in memory and bbdd is the same, my memory object is changed with what is in bbdd.

    //Operaciones operacionMemoria --> Este objeto contiene los datos en memoria, diferentes de los de BBDD.

    OperacionesDaoImpl dao = new OperacionesDaoImpl(EntityManager);
    OperacionesId idOperacion = new OperacionesId(idOperacion, beo); 
    Operaciones operacionbbdd = null;
    try { 
        operacionbbdd = dao.getOperacionById(idOperacion);

It is just after consulting the operation in BBDD, which also changes the Memory operation that had the changes.

Any ideas?

Thank you very much.

    
asked by Marea 26.09.2017 в 13:38
source

1 answer

-1

Surely your application reads this object and Hibernate keeps it linked to the model, so when you ask again reuse the existing instance, updating the fields.

One solution is, when you get the version you want to keep apart from the database, make a manual copy of it.

The other solution is to use Session.evict() for Hibernate to "forget" that instance.

    
answered by 26.09.2017 в 13:57