Hibernate silently overwrites stale data

My code was right. Hibernate disagreed. Last week, while building my side project, I hit a bug that had no error. No exception. No stack trace. Just... missing data. The flow looked clean — Save the bio profile. Then mark the user as bio-completed. Two saves. Straightforward. But every time I ran it, the bio data was getting wiped silently. Hibernate wasn't complaining. It was just quietly overwriting what I'd saved — because currentUser was loaded before the bio was written. Its in-memory state was stale. And when I called userRepository.save(currentUser), Hibernate trusted that stale object completely and flushed it back to the database. Classic entity state pollution. Silent. No warnings. The minimal fix would've been reordering the saves. I didn't go with that. Instead I wrote a surgical JPQL update — touching only the one column I needed to change. No entity graph. No dirty checking. No surprises. Hibernate is powerful. But it trusts your object state completely. If your object is stale, your database pays the price — quietly. If you're calling multiple repository.save() in one flow, ask yourself: "Does any of these entities hold a stale reference to something the other just wrote?" It's a question worth asking before Hibernate answers it for you. #Java #SpringBoot #SoftwareEngineering

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories