Hibernation is a persistent entity during a flash when the object has not changed

I had a problem when the object manager saves an object, which, I think, has not changed during the flash.

I know that the following code is a problem, because if I comment on this, the object is not saved. In this code, all I do is load an object and call some getters.

Query qry = em.createNamedQuery("Clients.findByClientID");
qry.setParameter("clientID", clientID);

Clients client = (Clients) qry.getSingleResult();

results.setFname(client.getFirstName());
results.setLname(client.getLastName());
...
return results;

Later in another method, I do another namedQuery that forces the object manager to reset. For any reason, the client downloaded above is saved.

The reason this is a problem is because in the middle of it all there is some old code that makes some direct JDBC changes to the client. When the object manager is saved, changes made by direct JDBC are lost.

, , , , , , .

- , ?

+3
3

, ( , , session.clear() ..), , .

- , , flush() , , .

, , equals() hashCode() - Hibernate, , , , , , , .

( ) , hashCode() - Student - Teacher, Teacher . dirty- a Student, Hibernate Teacher, , (- ), , Student , , Teacher ID.

, hashCode() equals() , / , .

+1

, , . , hibernate db, .

, readOnly - hibernate, , .

   session.setReadOnly(client, true);

, , Session.evict(). , , db, , JDBC.

+2

If your recipient is executing any logic, so that it returns something else, except that the sleep mode is sent to the setter, then it will be marked as dirty. For example, if your getter logic returns 0 when hibernate gave you zero from the database, then it will be marked dirty because it looks like it has changed. I don’t know how to tell hibernate not to notice this change, since the object was dirty.

+1
source

Source: https://habr.com/ru/post/1746096/


All Articles