7

I'm using Spring Boot 1.5.4, Spring Data REST, Spring JPA, Hibernate and I'm developing a Angular client consuming REST API.

Spring Data REST helps a lot and I'm trying to follow best practice, so a repository is like:

@Transactional
@PreAuthorize("isAuthenticated()")
public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long> {
}

and automagically I've all my save(), delete(), findXX() methods. That's great.

Now I'm wondering how if I need custom business logic to do before the entity is saved. let's say I need to do some kind of complex validation (involving queries on the db), and other backstage activities (maybe saving related entities, updating related objects, ect). My goals are:

  1. Ensure every time the entity is saved (either from a REST call or a JPA call) my business logic is called before the object is saved
  2. Avoid to create a custom repository because a developer could call the standard repository breaking my rules
  3. Find a way to do this in a simple way in order to keep the app easy to mantain

The @RepositoryEventHandler is not enough for me because I want ensure my business logic is always verified even when the call to the method come from internal classes.

Could you suggest me the best approach to reach my goals?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
drenda
  • 5,846
  • 11
  • 68
  • 141
  • 1
    Hello! About using AOP - maybe [this](https://stackoverflow.com/a/26294838) will be helpful... – Cepr0 Jul 12 '17 at 19:27

1 Answers1

14

JPA has a bunch of entity listener.

@PrePersist Executed before the entity manager persist operation is actually executed or cascaded. This call is synchronous with the persist operation.
@PreRemove  Executed before the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation.
@PostPersist    Executed after the entity manager persist operation is actually executed or cascaded. This call is invoked after the database INSERT is executed.
@PostRemove Executed after the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation.
@PreUpdate  Executed before the database UPDATE operation.
@PostUpdate Executed after the database UPDATE operation.
@PostLoad   Executed after an entity has been loaded into the current persistence context or an entity has been refreshed.
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
  • 1
    thanks for the reply. I know entity listeners but I don't know if that is the best way to reach my goal. Somewhere I read to avoid to put a lot of business logic and queries in entity listeners. What do you think about? Maybe would be better use Spring AOP? – drenda Jul 12 '17 at 19:16
  • @drenda You didn't mention in your question that you are aware of entity listeners. You asked for a way to execute business logic before DB operations . If your question is on choosing one of many designs, this is the wrong forum to ask that. If I were you, I'd avoid reinventing the wheel by writing AOP for what is already there. – Abhijit Sarkar Jul 12 '17 at 19:25
  • 1
    I didn't mention I am aware of AOP too.I didn't ask for A WAY to do that,I asked for the best approach considering I have to do queries and complex business logic and I don't want do something "discouraged".I don't agree with you:this forum is very helpful also to get help from developers to use best practice;many times who developed libraries clarified how to use that in the best way.Thanks for your reply,but maybe could be someone more expert than me and you having more/different considerations to do about this use case that could be useful also for other people. – drenda Jul 12 '17 at 20:03
  • @drenda, This forum is dedicated to specific technological questions, not pedagogical design questions. There is definitely a place for such discussions, but this isn't that. It has got nothing to do with how much I know or don't. – Abhijit Sarkar Jul 12 '17 at 21:27