How to get old entity value in @HandleBeforeSave event to determine if a property is changed or not?

后端 未结 10 1754
余生分开走
余生分开走 2020-12-13 13:33

I\'m trying to get the old entity in a @HandleBeforeSave event.

@Component
@RepositoryEventHandler(Customer.class)
public class CustomerEventHan         


        
10条回答
  •  余生分开走
    2020-12-13 14:08

    You're currently using a spring-data abstraction over hibernate. If the find returns the new values, spring-data has apparently already attached the object to the hibernate session.

    I think you have three options:

    1. Fetch the object in a separate session/transaction before the current season is flushed. This is awkward and requires very subtle configuration.
    2. Fetch the previous version before spring attached the new object. This is quite doable. You could do it in the service layer before handing the object to the repository. You can, however not save an object too an hibernate session when another infect with the same type and id it's known to our. Use merge or evict in that case.
    3. Use a lower level hibernate interceptor as described here. As you see the onFlushDirty has both values as parameters. Take note though, that hibernate normally does not query for previous state of you simply save an already persisted entity. In stead a simple update is issued in the db (no select). You can force the select by configuring select-before-update on your entity.

提交回复
热议问题