Spring Data REST Events for Entity Association updates

842 Views Asked by At

Are there any spring data rest event handlers available for when a new entity association is created ? I.e. PUT a to an association resource url.

My entity class with the relationship is:

public class Group{
  @ManyToMany
  @JoinTable(name = "user_groups",
           joinColumns = @JoinColumn(name = "group_id"),
           inverseJoinColumns = @JoinColumn(name = "user_id")
  )
  private List<User> users;
}

I have tried a PUT to /groups/[id]/users, and this will attach the user to the group successfully, but I can not find an event handler that can be used after this type of event.

I have also tried updating with a PUT to /groups/[id]/users using a links object, which also attaches the object successfully:

{"_links":
  {"users": 
    {"href" : "http://localhost:8084/users/1"} 
  }
}

My two attempted event handlers are:

@HandleAfterLinkSave
public void handleGroupUpdate(Group g, User u){
    log.debug("User "+u.getUsername() +"added to group "+g.getName());
}

@HandleAfterLinkSave
public void handleGroupUpdateGen(Object g, Object u){
    log.debug("User "+u.toString() +"added to group "+g.toString());
}

Events: http://docs.spring.io/spring-data/rest/docs/current/reference/html/#events

Association resources: http://docs.spring.io/spring-data/rest/docs/2.3.0.RELEASE/reference/html/#repository-resources.association-resource

1

There are 1 best solutions below

3
On

The proper parameters for the text/uri-list would be

public void handleGroupUpdate(Group g, List<User> u)

This aligns with the users field definition inside of the Group class.