This is my SQL statement which works properly:
SELECT cedula FROM paciente INNER JOIN titular ON paciente.id = titular.paciente_id WHERE cedula = '19163676';
returns cedula
field from paciente
table when the ids match
I've read this post: How To Define a JPA Repository Query with a Join
But in that post the relationship is One-to-One.
In my case, the relationship is Many-to-One where the father model is paciente
and the child model is titular
This is my Paciente domain:
Paciente.java
@Id
@Column(name="pacienteId")
@SequenceGenerator(name="paciente_id_seq",
sequenceName="paciente_id_seq",
allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator="paciente_id_seq")
private Long id;
@NotNull
@Column(name = "cedula", nullable = false)
private String cedula;
And this is my Titular domain with the relationship:
Titular.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name="pacienteId")
private Paciente paciente;
And this is my PacienteRepository.java which will contain the query:
public interface PacienteRepository extends JpaRepository<Paciente,Long> {
List<Paciente> findByNombreContainsIgnoreCase(String nombre);
List<Paciente> findByNombreLikeIgnoreCaseAndApellidoLikeIgnoreCase(String nombre, String apellido);
List<Paciente> findByCedula(String cedula);
Page<Paciente> findByCedulaStartsWith(String cedula, Pageable pageable);
}
How can I do it?