Java - Spring boot query that return an object made from two different tables

gregor vojvoda :

I have two entities that have references by id.

One entity is a candidate:

@Entity
@Table(name = "eupass_candidate", schema = "hrast")
public class Candidate{

    private long id;
    private String firstName;
    private String lastName;
    private String address;
    private String munic;
    private String postalCode;
    // getters and setters

}

And the other one is

@Entity
@Table(name = "eupass_pdffile", schema = "hrast")
public class PDFFile implements Serializable {

private static final long serialVersionUID = -753514667628201960L;
private long id;
private byte[] pdfFile;
private Long idCandidate;

Now I need to do a join of this two tables and retrieve the candidate first name, last name and the pdf if it has any.

I managed this with the following query:

SELECT fname, lname, pdf_file 
FROM hrast.eupass_candidate  
LEFT OUTER join hrast.eupass_pdffile 
     ON (hrast.eupass_candidate.id = hrast.eupass_pdffile.id_candidate)

Finally I would like to save the result in an object and send it to the front end application.

I understand that I can write the query annotation in an extended CrudeReopository, but in witch one.

  • Do I need another object
  • Or does java let you create an in place object just for this purpose
medkhelifi :

what about this approach?

@Entity
@Table(name = "eupass_pdffile", schema = "hrast")
public class PDFFile implements Serializable {

    private static final long serialVersionUID = -753514667628201960L;
    private long id;
    private byte[] pdfFile;
    private Long idCandidate;
    //@ManyToMany
    @JoinColumn(name="id_candidate") //or whatever your column name is.
    private Candidate candidate ;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=102041&siteId=1