Spring Data MongoDB not able to load reference collection (@DocumentReference)

1k Views Asked by At

In Spring data findAll method reference object is coming as null

I am using Reactive Mongo Repository

Ex Parent Object

@Data
@Document(collection = "country")
public class CountryBean {

    @Id
    private String id;
    
    private String name;
    
}

Child Object

@Document(collection = "city")
public class CityBean {

    @Id
    private String id;
    
    @Field(name = "name")
    private String name;
    
    @Field(name = "city_code")
    private String cityCode;
    
    @Field(name = "show_city")
    private boolean showCity;
    
    @DocumentReference(lazy = false)
    private StateBean state;
}

Country Collection enter image description here

State Collection (Here we can see the country Attribute)

enter image description here

But When trying to fetch from DB, I am getting country attribute as null. Tried both lazy true/false, but not getting country object along with state object.

@GetMapping("/get-all-state")
    Flux<StateBean> allState() {
        Flux<CountryBean> ct = countryRepository.findAll();
        Flux<StateBean> bean=  stateRepository.findByCountry(ct.blockFirst());
        return bean;
    }
[{"id":"6237a912850ceb6261998a53","name":"Bangalore","statecode":"39","country":null},{"id":"6237a94a850ceb6261998a55","name":"delhi","statecode":"39","country":null}]
0

There are 0 best solutions below