我有一个主实体类,其中包含以下字段,其中有一个字段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的患者。任何人都可以分享这种方法吗?
答案 0 :(得分: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的患者:
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();