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")