Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data REST MongoDB: Retrieve objects of DBRef instead of href

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!

like image 871
StealthyDev Avatar asked Mar 13 '23 22:03

StealthyDev


1 Answers

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.

like image 115
Francesco Pitzalis Avatar answered Apr 06 '23 02:04

Francesco Pitzalis