Java Spring数据REST MongoDB:检索DBRef而不是href的对象
各位专家您好@stackOverflow 我们正在使用Spring数据REST MongoDB 是否可以加载子对象,而不是使用@DBRef注释的超链接?请参阅下面的Java Spring数据REST MongoDB:检索DBRef而不是href的对象,java,spring,mongodb,rest,spring-data-rest,Java,Spring,Mongodb,Rest,Spring Data Rest,各位专家您好@stackOverflow 我们正在使用Spring数据REST MongoDB 是否可以加载子对象,而不是使用@DBRef注释的超链接?请参阅下面的流程.模板属性 这是我们的模型: import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.data.mongodb.core
流程.模板属性
这是我们的模型:
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<>();
带来了以下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)
,但没有运气
提前谢谢 您有两种可能:
将您的合并结果
存储在流程
文档中(我不知道它是否适用于您的情况,但即使您有很多合并结果
也是最佳选择,因为@DBRef
类似于SQL连接,而MongoDB在这方面并不擅长)
使用
使用摘录
您可以通过以下步骤实现目标:
1) 创建流程文档的投影
@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();
}
@Projection(name=“inlineTemplates”,types={Process.class})
InlineTemplates接口{
字符串getId();
字符串getName();
//在投影中使用getTemplates()会使信息内联
列出getTemplates();
}
2) 编辑您的存储库
@RepositoryRestResource(excerptProjection = InlineTemplates.class)
interface ProcessRepository extends CrudRepository<Process, String> {}
@RepositoryRestResource(摘录projection=InlineTemplates.class)
接口ProcessRepository扩展了Crudepository{}
3) 转到http://localhost:8080/data/process
查看结果
注意:我没有尝试代码,只是按照文档中的说明进行操作。如果它不起作用,很抱歉。太好了!我不得不使用摘录。非常感谢。我没有使用摘录投影进行注释,而是在执行时使用了?projection=inlineTemplates。
@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();
}
@RepositoryRestResource(excerptProjection = InlineTemplates.class)
interface ProcessRepository extends CrudRepository<Process, String> {}