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 the Process
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 of MergeResult
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
@Projection(name = "inlineTemplates", types = { Process.class })
interface InlineTemplates {
String getId();
String getName();
// using getTemplates() inside a projection causes the information to be inlined
List<MergeTemplate> getTemplates();
}
2) Edit your repository
@RepositoryRestResource(excerptProjection = InlineTemplates.class)
interface ProcessRepository extends CrudRepository<Process, String> {}
3) Go to http://localhost:8080/data/process
to see the result
NOTE: I didn't try the code, just take the instructions from the documentation. Sorry if it doesn't work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With