0

I have the main entity class with the below fields where there is a field finid which references patient entity class. :-

public class Patientrel implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "patientrelid")
private Long patientrelid;
@Column(name = "entrydate")
@Temporal(TemporalType.TIMESTAMP)
private Date entrydate;
@JoinColumn(name = "finid", referencedColumnName = "fin_id")
@ManyToOne
private Patient finid;

Entity class of Patient :-

public class Patient implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "fin_id")
private Integer finId;
@Size(max = 100)
@Column(name = "patient_name")
private String patientName;
@OneToMany(mappedBy = "finid")
private Collection<Patientrel> patientrelCollection;

Now i need to search patientrel matching a given finid. Can anyone please share the approach for that?

Ranjith Nair
  • 475
  • 1
  • 6
  • 17

1 Answers1

0

Now i need to search patientrel matching a given finid.

First option is to get the Patient by finId, and then retrieve the Patientrel collection thru .getPatientrelCollection() getter method:

EntityManager em = ...
Integer findId = 1;

Patient patient = em.find(Patient.class, findId );
Collection<Patientrel> patientrelCollection = patient.getPatientrelCollection();

 

Second option is to to use a JQL query which joins Patient and Patientrel entities to "search" for patientrels matching a given finid:

EntityManager em = ...
Integer findId = 1;

Query q = entityManager.createQuery(
   "SELECT prel FROM " + Patient.class.getName() + "p " +
   "join p.patientrelCollection prel " + 
   "WHERE p.finId = :id");
q.setParameter("id", findId);

List<Patientrel> patientrel = (List<Patientrel>)q.getResultList();
Sergej Panic
  • 854
  • 5
  • 8
  • I guess i was not clear with the question raised. What i need is to get the list of patientrel matching a given finid. Now patientrel has finid as the foreign key to the table patient. – Ranjith Nair Oct 06 '13 at 09:46