jeudi 13 mai 2021

Spring Data Rest: Dynamic Relationship via Selector

I am working on a little app that requires entities to be grouped dynamically by their properties. Let's say I have a Thing entity and a Group entity. The Group entity has a Selector that implements boolean selects(Thing th), so the set Things in this Group changes as Things come and go.

Since Group is not hard wiring the relation to Thing, I need some advice how to best handle this. I can currently imagine the following:

A) Somehow autowire ThingRepository into Group and implement Group.getThings() by fetching all the Things and running them through the Selector. But I'm not sure if that autowiring is possible or a good pattern.

B) Declare a ThingRepository.findByGroup method with a custom implementation, but that takes away the convenience of just calling findAll since I can access the ThingRepo from the custom method.

C) Handle the situation by implementing a custom REST controller for the /groups/ID/things call.

What would you suggest and why? Are there other options I am not seeing?

Aucun commentaire:

Enregistrer un commentaire