I am using Spring boot with MongoDB to define a OneToMany relationship between the Class Building and Zone (a Building contains many zones and each zone has an idBuilding field to specify to which building it belongs). In a typical MySQL-based syntax there's no problem (i define it using unidirectional OneToMany relationship between Building and Zone, but with mongoDB syntax i am having some trouble to do it. My question is how to define such unidirectional OneToMany relationship for MongoDB accounting for the CASCADE option (with or without using @DBRef)? and how to query them using the @Query annotation.
Here's my try for mongoDB for the relationships (i'm using the embbeded technique) :
@Document("Building")
public class Building {
private Zone[] zones;
...
}
@Document("Zone")
public class Zone {
private Building building;
...
}
AND for the query :
@Repository
public interface ZoneRepository extends MongoRepository <Zone, Long> {
@Query(value = "{'building.id': ?0}")
List<Zone> queryZoneByBuildingId(@Param("id") Long id);
}
Please suggest modification to this setup.