Spring Repository Projection 获取一个对象的子对象 [英] Spring Repository Projection Get Child of One Object

查看:54
本文介绍了Spring Repository Projection 获取一个对象的子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Spring Repository interfaces a lot. One was working fine for me but then I realized I needed a little bit more from it. I wanted to go one more level of get()

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

I work on an intranet so can't copy and paste but hopefully the following will give enough info to make it understandable...

@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();
  ...
}

这是我现在得到的东西的代表......

Here is a representation of what i am getting now...

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

这是我想要的表示...

Here is a representation of what i want...

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

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

I know this is not correct but i basically want

@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();  
  ...
}

推荐答案

@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();  
  ...
}

这篇关于Spring Repository Projection 获取一个对象的子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆