0

I have a table patient_details(patient_id, first_name, last_name, address,date_of_birth, gender, contact_number,occupation). I have generated an entity class and a PersistenceUnit. I can only find an object using its ID:

PatientDetails pd = em.find(PatientDetails.class,patient_id);

I want to know how to find an object by using other column name(s) instead of just the primary key.

1fuyouinfinite
  • 1
  • 1
  • 1
  • 14

2 Answers2

0

For example:

if

class PatientDetails
 String first_name;
 @Column(",date_of_birth")
 Date birth;
 ...
}

then

String sql = "SELECT p FROM PatientDetails p where p.first_name = :fname and p.birth > :generation";
Date generation = new Date(int 1980, 0, 1);
TypedQuery<PatientDetails> query = EM.createQuery(sql);
query.setParameter("fname","John");
query.setParameter("generation",generation);
return query.getResultList

returns patients called John and born after 1980.

But you should read the links recommended by @pL4Gu33

Zielu
  • 8,312
  • 4
  • 28
  • 41