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

后端 未结 10 1728
余生分开走
余生分开走 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条回答
  •  Happy的楠姐
    2020-12-13 14:25

    The following worked for me. Without starting a new thread the hibernate session will provide the already updated version. Starting another thread is a way to have a separate JPA session.

    @PreUpdate

    Thread.start {
        if (entity instanceof MyEntity) {
            entity.previous = myEntityCrudRepository.findById(entity?.id).get()
        }
    }.join()
    

    Just let me know if anybody would like more context.

提交回复
热议问题