Return all records from another class in JPA Spring

Asked

Viewed 87 times

0

How do I return a complete list of another class within a class? I need Patient.java to return (in the findALL that returns me in JSON) a list of all records in the Statuspaciente.java class, without restrictions. This is necessary because the front-end needs to know all the values (ID field and description) to fill a lookup for the user to select the value you want.

Follows the code:

@Entity
@Table(name="cad_paciente")
public class Paciente {

 ... (fields)

@ElementCollection
@CollectionTable(name="cad_status_paciente")
private List<StatusPaciente> getListaStatusPaciente;

    public List<StatusPaciente> getListaStatusPaciente(){
        TypedQuery<StatusPaciente>  em = EntityManager.createQuery("SELECT x FROM cad_status_paciente x");
        return em.getResultList();
    }

--x--

public class StatusPaciente {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name="ds_status")
    @Size(max=50)
    private String status;

How do I do that? What’s the best way?

  • Hello, this is the English OS, please translate your question.

  • Thanks for the feedback, translated the text..

  • the title was missing

  • Amended, thank you very much.

  • Have you thought about creating your own @Query on your Repository?

  • @Nicolaspereira Can you provide a simple example? Remembering that we need to return all Statuspaciente within the Patient’s JSON. It is possible?

  • You can create your own @Query as if it were a query in a database, then within the query you make a Join Inner as you want

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.