I use Spring Data Rest
over JPA
mappings.
JPA provides @PrePersist
annotation for methods to be called before the persistence of en entity in the DB.
Spring Data Rest provides @HandleBeforeCreate
annotation for method to be called when catching an entity creation event.
This seems rather equivalent to me. When should I use one and when should I use the other ?
Spring Data REST builds on top of Spring Data repositories, analyzes your application's domain model and exposes hypermedia-driven HTTP resources for aggregates contained in the model.
Spring Data REST can be used to expose HATEOAS RESTful resources around Spring Data repositories. Without writing a lot of code, we can expose RESTful API around Spring Data Repositories.
This is an interface that allows you to perform various operations with WebsiteUser objects. We also defined a custom query that will provide a list of users based on a given name. The @RepositoryRestResource annotation is optional and is used to customize the REST endpoint.
This repository is an interface that lets you perform various operations involving Person objects. It gets these operations by extending the PagingAndSortingRepository interface that is defined in Spring Data Commons. At runtime, Spring Data REST automatically creates an implementation of this interface.
For instance, I use @HandleBeforeCreate rather than @PrePersist for something like security checks. Since due to item 1, I only want to check the security for exposed rest operations and due to item 2, I can easily use @Secured or @PreAuth annotations with my methods to do the check.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With