0

Let's say there are two simple entities:

@Entity
public class Author {
    private String name;
    @ManyToMany
    private List<Book> books;

@Entity
public class Book {
    private String title;
    @ManyToMany
    private List<Author> authors;

Next I am sending a PATCH request to update the Author:

http://localhost:8080/authors/1
body:
{
    "books": [
        "http://localhost:8080/books/2",
        "http://localhost:8080/books/3"
    ]
}

There is a handler:

@RepositoryEventHandler
public class AuthorEventHandler {
    @HandleBeforeSave
    public void handleBeforeAuthorSave(Author author) {
        System.out.println("handleBeforeSave Author: " + author);
    }

    @HandleAfterSave
    public void handleAfterAuthorSave(Author author) {
        System.out.println("HandleAfterSave Author: " + author);
    }

    @HandleBeforeLinkSave
    public void handleBeforeLinks(Author author, List<Book> books) {
        System.out.println("handleBeforeLinks Author: " + author);
    }

    @HandleAfterLinkSave
    public void handleAfterLinks(Author author, List<Book> books) {
        System.out.println("handleAfterLinks Author: " + author);
    }
}

The handler catches only the AfterSaveEvent and BeforeSaveEvent. Spring does not produce Before-AfterLinkSave events.

EDIT:
I've noticed the method:

@RequestMapping(value = BASE_MAPPING, method = { PATCH, PUT, POST }
createPropertyReference()

With the BASE_MAPPING = "/{repository}/{id}/{property}"; So the PATCH request on the http://localhost:8080/authors/1/books triggers it. It's not the way I thought about from the start.

IgorZ
  • 1,125
  • 3
  • 20
  • 44

1 Answers1

1

Indeed Spring Data Rest doesn't produce Before-AfterLinkSave events; it's only either Before or After.

Of course without knowing what particular problem you are trying to solve it's hard to tell but if you need to handle Before-After = Aroud execution then you might want to look at Spring AOP using which you can create a Pointcut to repository save method and set up an Advice (basically interceptor) arond that method execution.

An advice is an action taken by an aspect at a particular Joinpoint. Different types of advice include “around,” “before” and “after” advice.

catie
  • 41
  • 4
  • I was trying to resolve 2 problems: first I expected to fire the `LinkEvents` on `/{repository}/{id}` instead of the `/{repository}/{id}/{property}` (but it is not possible). The second - adding transactional support on events. This https://stackoverflow.com/a/53236152/4207793 helped me to do it. – IgorZ Jul 03 '21 at 13:00
  • Great! So AOP was the way to go in the end. Glad you solved your problem. Good point abt transactional support, events are reaaly not supporting that connection to the method execution. – catie Jul 03 '21 at 13:09
  • Yes. Then only one difference that I've included only the two classes `webmvc.RepositoryEntityController` and `webmvc.RepositoryPropertyReferenceController` instead of the whole `data.rest.webmvc` package. (It would work in both cases) – IgorZ Jul 03 '21 at 13:17