Hibernate is persisting entity during flush when the entity has not changed

Posted by Preston on Stack Overflow See other posts from Stack Overflow or by Preston
Published on 2010-05-18T23:06:22Z Indexed on 2010/05/18 23:10 UTC
Read the original article Hit count: 262

Filed under:
|
|

I'm having a problem where the entity manger is persisting an entity that I don't think has changed during the flush.

I know the following code is the problem because if I comment it out the entity doesn't persist. In this code all I'm doing is loading the entity and calling 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 a different method I do another namedQuery which causes the entity manger to flush. For some reason the client loaded above is persisted.

The reason this is a problem is because in the middle of all this, there is some old code that is making some straight JDBC changes to the client. When the entity manger persists the changes made by the straight JDBC are lost.

The theory we have at moment is that the entity manger is comparing the entity to the underlying record, sees that it's different, then persists it.

Can someone explain or confirm the behavior we're seeing?

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about entity