0

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();  
  ...
}
user2052618
  • 556
  • 2
  • 7
  • 20
  • check this please https://stackoverflow.com/questions/44554979/how-to-loop-and-retrieve-value-from-hateoas-link-attribute-e-g-retrieve-a-des/44564464#44564464 – Amr Alaa Nov 29 '17 at 03:38

1 Answers1

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();  
  ...
}
Amr Alaa
  • 545
  • 3
  • 7
  • This worked! One minor thing is that you can't do getRequisite().getDocument() from what i can tell which is fine. just Document getDocument() and annotate it as you mentioned. so now my problem is too much data. The Document object has the binary of the document and that comes back as well. i probably have to just get the pieces of the document i want with the Value annotation. This just shows that we should of put the document on disk and the database have a pointer to the document. We discussed this but thought not to do it, at least now. – user2052618 Nov 29 '17 at 16:19
  • i added JsonIgnore to the field that had the document binary. so everything is grand again! thanks! – user2052618 Nov 29 '17 at 16:27