Real World Example

Price-Change History for a Marketplace

Marketplaces like Amazon and Flipkart must show sellers and support staff who changed a price and when, and regulators may ask for proof of the price on a sale date. Audit columns tell you only the last change. ShopEase keeps a history table filled by an entity listener, plus Hibernate Envers for full revision history.

@Component
public class PriceHistoryListener {

    private static ApplicationEventPublisher events;            // listeners are created by Hibernate

    @Autowired
    void init(ApplicationEventPublisher publisher) { PriceHistoryListener.events = publisher; }

    @PostLoad
    void remember(Product p) { p.rememberLoadedPrice(); }       // stores price in a @Transient field

    @PreUpdate
    void onUpdate(Product p) {
        if (p.priceChanged()) {
            events.publishEvent(new PriceChanged(p.getId(), p.getLoadedPrice(), p.getPrice()));
        }
    }
}

@Component
@RequiredArgsConstructor
class PriceHistoryWriter {
    private final PriceHistoryRepository history;
    private final AuditorAware<String> auditor;

    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)   // same transaction
    void on(PriceChanged e) {
        history.save(new PriceHistory(e.productId(), e.oldPrice(), e.newPrice(),
                                      auditor.getCurrentAuditor().orElse("system")));
    }
}

The Result

Example

curl localhost:8080/api/admin/products/7/price-history

Example output

HTTP/1.1 200 OK
[
  {"oldPrice": 1299.00, "newPrice": 1099.00, "changedBy": "[email protected]", "changedAt": "2026-03-10T06:30:00Z"},
  {"oldPrice": 1099.00, "newPrice": 999.00,  "changedBy": "pricing-bot",            "changedAt": "2026-03-14T18:30:00Z"}
]

Tip: For complete history of every column, add org.hibernate.orm:hibernate-envers and annotate the entity with @Audited. Envers writes each change into a products_aud table with a revision number, and you can query the entity "as of" any revision.