Hibernate is persisting entity during flush when the entity has not changed
- by Preston
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?