我正在使用 Spring Boot 和 Spring Data,但在尝试使用 JPA 和 EntityGraph 加载实体时遇到问题。我有一个患者和保险实体。每个患者可以有多个保险,每个保险可以分配给多个患者。我决定使用加入表 PatientInsurance,因为我需要存储额外的字段,如“活动”,以及关系代码(患者可以是该特定保险的成员、配偶或孩子)。
使用 Spring Data 存储库,我使用 EntityGraph 注释了查找患者的方法,以便在一个查询中为该患者准备好 PatientInsurances(和保险)列表。
这是代码(我删除了范围内不必要的部分)
病人等级
@Entity
@Table(name = "patient")
public class Patient {
@NotNull
@NotEmpty
@Column(length = 60, nullable = false)
private String patientFirstName;
@NotNull
@NotEmpty
@Column(length = 60, nullable = false)
private String patientLastName;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "patient", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
List<PatientInsurance> patientsInsurances = new ArrayList<>();
public void addPatientInsurance(PatientInsurance patientIns) {
if (!patientsInsurances.contains(patientIns)) {
patientsInsurances.add(patientIns);
}
}
//other properties...
保险类
@Entity
@Table(name = "insurance")
public class Insurance {
@Column(name = "policy_id", length = 20)
private String policyId;
@OneToMany(mappedBy = "insurance", fetch = FetchType.LAZY,cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
private List<PatientInsurance> patientsInsurances = new ArrayList<PatientInsurance>();
public void addPatientInsurance(PatientInsurance patientIns) {
if (!patientsInsurances.contains(patientIns)) {
patientsInsurances.add(patientIns);
}
}
//other properties
患者和保险之间的连接表的实体(需要一个连接表用于该实体中的额外字段,如 active 和 relCode
@Entity
@IdClass(PatientInsurance.PatientInsurancePK.class)
@Table(name = "patient_insurance")
public class PatientInsurance implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "patient_id")
private Patient patient;
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "insurance_id")
private Insurance insurance;
@Column(name = "active")
private boolean active;
@Column(length = 1)
private String relCode;
public PatientInsurance() {
insurance = new Insurance();
patient = new Patient();
}
public PatientInsurance(Patient p, Insurance i, boolean active, String relCode) {
this.patient = p;
this.insurance = i;
this.active = active;
this.relCode = relCode;
p.addPatientInsurance(this);
i.addPatientInsurance(this);
}
public Patient getPatient() {
return patient;
}
public Insurance getInsurance() {
return insurance;
}
public void setInsurance(Insurance insurance) {
this.insurance = insurance;
insurance.addPatientInsurance(this);
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public void setPatient(Patient patient) {
this.patient = patient;
patient.addPatientInsurance(this);
}
public String getRelCode() {
return relCode;
}
public void setRelCode(String relCode) {
this.relCode = relCode;
}
static public class PatientInsurancePK implements Serializable {
protected Patient patient;
protected Insurance insurance;
public PatientInsurancePK() {
}
public PatientInsurancePK(Patient patient, Insurance insurance) {
this.patient = patient;
this.insurance = insurance;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PatientInsurancePK)) return false;
PatientInsurancePK that = (PatientInsurancePK) o;
if (!patient.equals(that.patient)) return false;
return insurance.equals(that.insurance);
}
@Override
public int hashCode() {
int result = (patient != null) ? patient.hashCode() : 0;
result = 31 * result + ((insurance != null) ? insurance.hashCode() : 0);
return result;
}
}
}
患者服务的实施
@Transactional
@Service("patientService")
public class PatientServiceImpl implements PatientService {
@Autowired
PatientRepository patientRepository;
@Override
public Optional<Patient> findByIdFull(Long id) {
Optional<Patient> patient = patientRepository.findById(id);
return patient;
}
//other methods...
患者资料库
public interface PatientRepository extends JpaRepository<Patient, Long> {
@EntityGraph(
attributePaths = {
"patientsInsurances",
"patientsInsurances.patient",
"patientsInsurances.insurance"},
type = EntityGraph.EntityGraphType.LOAD)
Optional<Patient> findById(Long id);
调用 PatientService 中的方法的代码片段
Optional<Patient> patientOptional = patientService.findByIdFull(p.getId());
if (patientOptional.isPresent()) {
Patient patient1 = patientOptional.get();
List<PatientInsurance> patientInsurances = patient1.getPatientInsurances();
PatientInsurances patientInsurance = patientInsurances.get(0);
Patient patient2 = patientInsurance.getPatient(); //and this is same istance of patient1, it's ok
Insurance insurance = patientInsurance.getInsurance();
//here is the problem!!!
insurance.getPatientInsurances();
//Unable to evaluate the expression Method threw 'org.hibernate.LazyInitializationException' exception.
所以问题似乎是当我进入患者侧时,我可以毫无问题地循环进入他的保险,但是当我尝试从保险实例开始做同样的事情时,我无法进入它的患者,因为他们是懒惰的加载。那么如何让jpa以正确的方式下载全图呢?