【问题标题】:Spring Repository Projection Get Child of One ObjectSpring Repository Projection 获取一个对象的子对象
【发布时间】:2018-05-12 13:54:29
【问题描述】:

我经常使用 Spring Repository 接口。一个对我来说工作得很好,但后来我意识到我需要更多一点。我想再上一层 get()

我在 Intranet 上工作,因此无法复制和粘贴,但希望以下内容能够提供足够的信息以使其易于理解...

@Entity
@Table(name="person")
class Person {
  ...
}

@Entity
@Table(name="requisite")
class Requisite {
  ...
  @OneToOne
  @JoinColumn
  private Document document;
}

@Entity
@Table(name="person_requisite")
class PersonRequisite {
  ...
  @ManyToOne
  @JoinColumn(name="person_id") 
  private Person person;
  ...
  @ManyToOne
  @JoinColumn(name="requisite_id") 
  private Requisite requisite;
  ...
}

@Projection(name="personRequisiteProjection", types={PersonRequisite.class})
public interface PersonRequisiteProjection {
  ...
  Person getPerson();
  Requisite getRequisite();
  ...
}

这是我现在得到的表示......

"personRequisites" : [ {
   ...
   "requisite" : {
     id : 1,
     ...
     no document object or document id from the requisite
    },
   "person" : {
     id : 33,
     ...
   },
   ...
 ]
...

这是我想要的表示......

"personRequisites" : [ {
   ...
   "requisite" : {
     id : 1,
     ...
     "document" : {
       "id" : 55,
       "name" : blah,
       ...
     }
    },
   "person" : {
     id : 33,
     ...
   },
   ...
 ]
...

我知道这是不正确的,但我基本上想要

@Projection(name="personRequisiteProjection", types={PersonRequisite.class})
public interface PersonRequisiteProjection {
  ...
  //i know, this would be out of place if it worked but trying to emphasize what i want...
  Document getRequisite().getDocument();
  //i'd still want Requisite getRequisite() as well but you get what i am after
  ...

  //or more appropriately, force document to show up in Requisite here...
  Requisite getRequisite();  
  ...
}

【问题讨论】:

  • 开什么玩笑。搜索 spring projection child 真的会引导您访问上面的链接吗?人们有严重的问题。他们是仇恨者。上帝会照顾你的。

标签: spring repository projection


【解决方案1】:
@Projection(name="personRequisiteProjection", types={PersonRequisite.class})
public interface PersonRequisiteProjection {
    ...
  @Value("#{target.requisite.document}")
  Document getRequisite().getDocument();
  //i'd still want Requisite getRequisite() as well but you get what i  am after
  ...

  //or more appropriately, force document to show up in Requisite   here...
  Requisite getRequisite();  
  ...
}

【讨论】:

  • 这成功了!一件小事是你不能做 getRequisite().getDocument() ,我可以说这很好。只需记录 getDocument() 并按照您提到的方式对其进行注释。所以现在我的问题是数据太多。 Document 对象具有文档的二进制文件,并且该文件也会返回。我可能只需要使用 Value 注释获取我想要的文档片段。这只是表明我们应该将文档放在磁盘上,并且数据库有一个指向该文档的指针。我们讨论过这个问题,但认为不这样做,至少现在是这样。
  • 我将 JsonIgnore 添加到包含文档二进制文件的字段中。所以一切都再次盛大!谢谢!
猜你喜欢
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 2019-05-31
相关资源
最近更新 更多