I have question related to DBRef of MongoDB. Imagine this scenario:
Group{
...
"members" : [
{
"$ref" : "User",
"$id" : ObjectId("505857a4e4b5541060863061")
},
{
"$ref" : "User",
"$id" : ObjectId("50586411e4b0b31012363208")
},
{
"$ref" : "User",
"$id" : ObjectId("50574b9ce4b0b3106023305c")
},
]
...
}
So given group document has 3 user DBRef. Where in java class of Group, members is tagged with morphia as @Reference:
public class Group {
...
@Reference
List<User> members;
...
}
Question: When calling RequestFactory function getGroup().with("members") will RequestFactory get all members in ONLY 1 DB access ?
Or will Request factory make 3 DB access for each DBRef in Group document in the scenario given above?
Thank you very much in advance.
RequestFactory itself doesn't access the DB. What it'll do here is:
getMembers()
, as it was requested by the client through.with("members")
isLive
method, or if has noLocator
, call the entity'sfindXxx
with itsgetId()
(and check whethernull
is returned).The first step depends entirely on Morphia's implementation:
lazy = true
on your@Reference
, it won't matter whether RequestFactory callsgetMembers()
or not, the members will always be loaded.Group
and another 3 for the members; I don't think Morphia tries to optimize the number of queries to only make 1 query to get all 3 members at a time)The second step however depends entirely on your code.
Remember that RequestFactory wants you to have a single instance of an entity per HTTP request. As I understand it, Morphia has an
EntityCache
doing just that, but I suspect it could be bypassed by some methods/queries.