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

后端 未结 10 1733
余生分开走
余生分开走 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:21

    I had the same problem, but I wanted the old entity available in the save(S entity) method of a REST repository implementation (Spring Data REST). What I did was to load the old entity using a 'clean' entity manager from which I create my QueryDSL query:

        @Override
        @Transactional
        public  S save(S entity) {
            EntityManager cleanEM = entityManager.getEntityManagerFactory().createEntityManager();
            JPAQuery query = new JPAQuery(cleanEM);
    //here do what I need with the query which can retrieve all old values
            cleanEM.close();
            return super.save(entity);
        }
    

提交回复
热议问题