Following structure:
@Document
public class Palace {
@Id
private String id;
private Location location;
// additional attributes, getter, setter
}
.
public class Location {
private String id;
// additional attributes, getter, setter
}
As far as I understand the concept of ObjectId, there can be only one ObjectId (_id) in a MongoDB document and it must be at the root level of the document. But when inserting data via Spring Boot 1.5.14 I get the followng structure:
{
"_id" : ObjectId("5b05387a8db58e0001d38851"),
"location" : {
"_id" : ObjectId("5ae712d1b2b197000132cd9b"),
}
}
My question is: Is this the expected behaviour in spring-data-mongo? I would have expected the following structure:
{
"_id" : ObjectId("5b05387a8db58e0001d38851"),
"location" : {
"id" : "5ae712d1b2b197000132cd9b",
}
}
If I annotate the Location id with @Field
public class Location {
@Field("id")
private String id;
// additional attributes, getter, setter
}
then the Document is saved as expected, but querying with repository method
getPalaceByLocationId()
won't give any results.
getPalaceByLocation_Id()with@Field. See if it works?getPalaceByLocation_Id()has the same result. Maybe this is a bug in spring-data-mongo, or the magic how to identify a MongoDb ObjectId is searching for ID fields in nested classes which is imho wrong.@Field("id"). Cross check.