[英]Three entities mapping is not working properly because of lazy/eager loading in JPA
我正在映射三个实体。 医生,客户(扩展人员)和医疗咨询。 参见上面的代码。 考虑具有默认构造函数的所有模型类,具有所有字段以及getter和setter的构造函数:
@Entity
@Table (name= "person")
public abstract class Person {
@Id @GeneratedValue
protected Long id;
protected String name;
protected String email;
protected String password;
@OneToOne
protected Address address;
现在班上的医生。
@Entity(name = "doctor")
public class Doctor extends Person{
@OneToMany(mappedBy="doctor" , fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JsonManagedReference(value = "job-historical")
private List<MedicalConsultation> medicalConsultations;
@Enumerated
private Type type;
@ElementCollection
private List<String> specialties;
public Doctor() {
super();
}
public Doctor(String name, String email, String password, Address address,
List<String> specialties, Type type,
List<MedicalConsultation> medicalConsultations) {
super(name,email,password,address);
this.setMedicalConsultations(medicalConsultations);
this.setSpecialties(specialties);
this.setType(type);
}
我的构造函数调用super()
并根据超类及其自身的属性设置其值。 Client类也会发生同样的情况。
@Entity(name = "client")
public class Client extends Person{
@JsonManagedReference(value = "client-consultations-historical")
@OneToMany(mappedBy="doctor" , fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<MedicalConsultation> medicalConsultations;
这里是医疗咨询模型,它获得了其他两个模型
@Entity
@Table(name = "medical_consultation")
public class MedicalConsultation {
@Id
@GeneratedValue
private Long id;
@JsonBackReference(value = "job-historical")
@ManyToOne
@JoinColumn(name="doctor_fk")
private Doctor doctor;
@ManyToOne
@JoinColumn(name="client_fk")
@JsonBackReference( value = "client-consultations-historical")
private Client client;
@JsonFormat(pattern = "dd/MM/yyyy hh:mm")
private Date date;
private BigDecimal price;
最终,我们遇到了一个问题:在控制器类上,我无法获得medicalConsultations的完整数据。 也就是说,我得到了数据,ID和价格,但是由于某种原因我没有得到客户和医生。 但是,如果我调用方法getDoctor()
或getClient
并返回其中之一,则可以看到所有信息。
请参见RestControl类上的方法:
@RestController
public class Control {
@Autowired
private PersonRepository personRepo;
@Autowired
private ClientRepository clientRepo;
@Autowired
private AddressRepository addressRepo;
@Autowired
private DoctorRepository doctorRepo;
@Autowired
private MedicalConsultationRepository consultationRepo;
@GetMapping("consultations")
public List<MedicalConsultation> getConsultations() {
List<MedicalConsultation> consultations = this.consultationRepo.findAll();
return consultations;
}
映射可能有问题。 但是我将休眠设置为显示sql,这显然使所有查询都获得了我想要的一切。 看到:
Hibernate:
select
medicalcon0_.id as id1_2_,
medicalcon0_.client_fk as client_f4_2_,
medicalcon0_.date as date2_2_,
medicalcon0_.doctor_fk as doctor_f5_2_,
medicalcon0_.price as price3_2_
from
medical_consultation medicalcon0_
Hibernate:
select
client0_.id as id2_3_0_,
client0_.address_id as address_7_3_0_,
client0_.email as email3_3_0_,
client0_.name as name4_3_0_,
client0_.password as password5_3_0_,
address1_.id as id1_0_1_,
address1_.city as city2_0_1_,
address1_.number as number3_0_1_,
address1_.phone as phone4_0_1_,
address1_.street as street5_0_1_,
medicalcon2_.doctor_fk as doctor_f5_2_2_,
medicalcon2_.id as id1_2_2_,
medicalcon2_.id as id1_2_3_,
medicalcon2_.client_fk as client_f4_2_3_,
medicalcon2_.date as date2_2_3_,
medicalcon2_.doctor_fk as doctor_f5_2_3_,
medicalcon2_.price as price3_2_3_,
client3_.id as id2_3_4_,
client3_.address_id as address_7_3_4_,
client3_.email as email3_3_4_,
client3_.name as name4_3_4_,
client3_.password as password5_3_4_
from
person client0_
left outer join
address address1_
on client0_.address_id=address1_.id
left outer join
medical_consultation medicalcon2_
on client0_.id=medicalcon2_.doctor_fk
left outer join
person client3_
on medicalcon2_.client_fk=client3_.id
where
client0_.id=?
and client0_.dtype='client'
Hibernate:
select
doctor0_.id as id2_3_0_,
doctor0_.address_id as address_7_3_0_,
doctor0_.email as email3_3_0_,
doctor0_.name as name4_3_0_,
doctor0_.password as password5_3_0_,
doctor0_.type as type6_3_0_,
address1_.id as id1_0_1_,
address1_.city as city2_0_1_,
address1_.number as number3_0_1_,
address1_.phone as phone4_0_1_,
address1_.street as street5_0_1_,
medicalcon2_.doctor_fk as doctor_f5_2_2_,
medicalcon2_.id as id1_2_2_,
medicalcon2_.id as id1_2_3_,
medicalcon2_.client_fk as client_f4_2_3_,
medicalcon2_.date as date2_2_3_,
medicalcon2_.doctor_fk as doctor_f5_2_3_,
medicalcon2_.price as price3_2_3_,
client3_.id as id2_3_4_,
client3_.address_id as address_7_3_4_,
client3_.email as email3_3_4_,
client3_.name as name4_3_4_,
client3_.password as password5_3_4_
from
person doctor0_
left outer join
address address1_
on doctor0_.address_id=address1_.id
left outer join
medical_consultation medicalcon2_
on doctor0_.id=medicalcon2_.doctor_fk
left outer join
person client3_
on medicalcon2_.client_fk=client3_.id
where
doctor0_.id=?
and doctor0_.dtype='doctor'
有人可以告诉我什么吗?
尝试在MedicalConsultation上映射像波纹管这样的注释。
@ManyToOne(fetch = FetchType.EAGER)
private Doctor doctor;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.