Spring Data REST MongoDB: Retrieve objects of DBRe

2019-07-05 05:49发布

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!

1条回答
霸刀☆藐视天下
2楼-- · 2019-07-05 06:09

You have two possibilities:

  1. Store your MergeResults 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 MergeResults since @DBRef is something like a SQL join, and MongoDB isn't great at that)
  2. Use an Excerpt

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.

查看更多
登录 后发表回答