Search code examples
springspring-dataspring-data-jdbc

@PostLoad and @PrePersist in Spring Data JDBC project


Does Spring Data JDBC have anything similar to @PostLoad and @PrePersist from Spring Data JPA?


Solution

  • With Spring Data JDBC you currently can't annotate the entity directly. But there are life cycle listeners and callbacks that you can use for the same purpose.

    One of the examples given:

    @Component
    class UserCallbacks implements BeforeConvertCallback<User>,
                                            BeforeSaveCallback<User> {   
    
        @Override
        public Person onBeforeConvert(User user) {
            return // ...
        }
    
        @Override
        public Person onBeforeSave(User user) {
            return // ...
        }
    }