Hello experts @ stackOverflow,
We are using Spring Data REST MongoDB.
Is it possible to eager load the child Objects, instead of Hyperlinks - which are annotated using @DBRef? Please refer the Process.templates
attribute below.
Here is our Model:
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.ArrayList;
import java.util.List;
@Document(collection = "process")
public class Process {
@Id
private String id;
private String name;
@DBRef ///////// ------> This is the corresponding attribute <------
private List<MergeTemplate> templates = new ArrayList<>();
Here is our Repository:
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "process", path = "process")
public interface ProcessRepository extends MongoRepository<Process, String> {
}
FindAll API brings links to the child objects
http://localhost:8080/data/process
Brings the following JSON.
{
"_embedded" : {
"process" : [ {
"id" : "56d731b82b45ee21a0d2ab0a",
"name" : "application-kit",
"_links" : {
...,
/********** This is the attribute in question (templates) ************/
"templates" : {
"href" : "http://localhost:8080/data/process/56d731b82b45ee21a0d2ab0a/templates"
}
}
}, ...]
}
I even tried @DBRef(lazy=false)
, but there is no luck.
Thanks in advance!
You have two possibilities:
MergeResult
s in theProcess
documents (I don't know if it is applicable for your case, but it would be the best choice even if you have a lot ofMergeResult
s since@DBRef
is something like a SQL join, and MongoDB isn't great at that)Using an Excerpt
You can achieve your goal with the following steps:
1) Create a projection of your Process document
2) Edit your repository
3) Go to
http://localhost:8080/data/process
to see the resultNOTE: I didn't try the code, just take the instructions from the documentation. Sorry if it doesn't work.