Spring Repository Projection Get Child of One Obje

2019-08-22 03:51发布

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条回答
叛逆
2楼-- · 2019-08-22 04:25
@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();  
  ...
}
查看更多
登录 后发表回答