springmongodbspring-webfluxdbref

How to handle mongodb relation modeling with webflux?


I have an old-ish (3 years) service I need to maintain and just joined the team. It is a spring-webflux app with mongodb (4). All models are defined with lombok and where there are relations between them, @DBRef annotation was used. It works fine, but now I have to bump up spring (and the rest of the dependencies) and I just realized @DBRef is no longer supported.

Somehow, I understand the decision, but I couldn't find any straight forward alternative other than doing myself all the cascade operations.

So, is there any easier way to approach this?

Thanks.


Solution

  • @DBRef has been replaced by @DocumentReference some time ago.

    @DocumentReference: Applied at the field to indicate it is to be stored as a pointer to another document. This can be a single value (the id by default), or a Document provided via a converter.

    This very simple example shows how it works:

    
       public class Account {
         private String id;
         private Float total;
       }
      
       public class Person {
         private String id;
         @DocumentReference
         private List<Account> accounts;
       }
    
    

    For more details, have a look at the official docu:

    https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping-usage-annotations