Relationship of one column to several

1

I have an entity class of Medical name, in which it has an IDMedical column. It relates to the Attendance entity class, which in turn has a MedicalID column, which would be a foreign key of that first.

There is also in this table, Attendance, another column called IDMedicoIndica, which is also a foreign key in the Medical ID column of the Medical entity. That is, in the table Medical has only one column with ID of the doctors and in the care I need to have the id of the doctor who performs and what it indicates.

When I try to relate the same way I did with IDMedico, but changing the name of the column, I get an error saying that the IDMedicoIndica column could not be found in the Medical entity. How can I proceed and what is the best practice in this case?

Follow the code:

@Entity
@Table(name = "Sis_Medico")
@PrimaryKeyJoinColumn(name = "IDMedico", referencedColumnName = "IDPessoa")
public class Sis_Medico extends Sis_Pessoa {

    @Column(name = "IDMedico", insertable = false, updatable = false)
    private Long IDMedico;

        @OneToMany(mappedBy = "medico", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
        private List<Sis_Atendimento> atendimentos = new ArrayList<>();

        @OneToMany(mappedBy = "medicoIndica", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
        private List<Sis_Atendimento> atendimentosIndica = new ArrayList<>();

@Entity
@Table(name = "Sis_Atendimento")
public class Sis_Atendimento {

    @Id
    @Column(name = "IDAtendimento")
    private Long idAtendimento;

    @ManyToOne
    @JoinColumn(name = "IDMedico", 
                referencedColumnName = "IDMedico",
                foreignKey = @ForeignKey(name = "SisMedico_SisAtendimento"))
    private Sis_Medico medico;

    @ManyToOne
    @JoinColumn(name = "IDMedicoIndica", 
                referencedColumnName = "IDMedico",
                foreignKey = @ForeignKey(name = "SisMedico_SisAtendimento"))
    private Sis_Medico medicoIndica;

The code thus does until the deploye wheel, but the problem is that when I try to call the IDMedicoIndica column of Answer in a @Query it gives error.

@Query("select Atendimento.idAtendimento, Atendimento.dataAtendimento, \n"
            + "paciente.IDPaciente, medico.IDMedico, Atendimento.IDMedicoIndica, Atendimento.desativado \n" 
            + "from Sis_Atendimento Atendimento")
    
asked by anonymous 18.05.2018 / 18:32

1 answer

0

When using JPA entities and writing a JPQL, you need to reference the names of the columns in the entity and not the name of the columns in the table, unless you want to do a native query (< in> native query ).

If you want to run a SQL and not a JPQL, just include the nativeQuery attribute with the true value and use the actual names of the tables and columns:

@Query("select Atendimento.idAtendimento, Atendimento.dataAtendimento, "
        + "Atendimento.IDMedico, Atendimento.IDMedicoIndica, Atendimento.desativado " 
        + "from Sis_Atendimento Atendimento", nativeQuery = true)

If you want to use JPQL, do:

@Query("select at.idAtendimento, at.dataAtendimento, "
        + "medico.IDMedico, at.IDMedicoIndica, at.desativado " 
        + "from Sis_Atendimento at JOIN at.medico medico", nativeQuery = true)

PS: I removed the part of paciente.IDPaciente because I did not share the patient relationship with the rest of the tables, but the idea is the same.

    
27.07.2018 / 18:28