0

我的主要实体类具有以下字段,其中有一个引用患者实体类的字段 finid。:-

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;

患者的实体类:-

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;

现在我需要搜索与给定 finid 匹配的患者关系。谁能分享一下这个方法?

4

1 回答 1

0

现在我需要搜索与给定 finid 匹配的患者关系。

第一种选择是通过 finId 获取 Patient,然后通过 .getPatientrelCollection() getter 方法检索 Patientrel 集合:

EntityManager em = ...
Integer findId = 1;

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

 

第二种选择是使用 JQL 查询,它连接 Patient 和 Patientrel 实体以“搜索”与给定 finid 匹配的 patientrel:

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();
于 2013-10-05T20:37:03.283 回答