簡體   English   中英

Spring Data REST MongoDB:檢索DBRef的對象而不是href

[英]Spring Data REST MongoDB: Retrieve objects of DBRef instead of href

你好專家@ stackOverflow,

我們正在使用Spring Data REST MongoDB。

是否有可能急於加載子對象,而不是使用@DBRef注釋的超鏈接? 請參閱下面的Process.templates屬性。

這是我們的模型:

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<>();

這是我們的存儲庫:

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帶來了子對象的鏈接

http://localhost:8080/data/process

帶來以下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"
        }
      }
    }, ...]
}

我甚至試過@DBRef(lazy=false) ,但沒有運氣。

提前致謝!

你有兩種可能性:

  1. MergeResult存儲在Process文檔中(我不知道它是否適用於您的情況,但即使您有很多MergeResult ,它也是最佳選擇,因為@DBRef類似於SQL連接和MongoDB)那不是很好)
  2. 使用摘錄

使用摘錄

您可以通過以下步驟實現目標:

1)創建Process文檔的投影

@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)編輯您的存儲庫

@RepositoryRestResource(excerptProjection = InlineTemplates.class)
interface ProcessRepository extends CrudRepository<Process, String> {}

3)轉到http://localhost:8080/data/process查看結果

注意:我沒有嘗試代碼,只需要從文檔中獲取說明。 對不起,如果它不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM