Spring Repository Projection Get Child of One Object

岁酱吖の 提交于 2019-12-12 04:46:47

问题


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

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

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


来源:https://stackoverflow.com/questions/47542914/spring-repository-projection-get-child-of-one-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!