由于JPA中的延迟/急切加载,三个实体的映射无法正常工作

时间:2018-11-27 13:12:48

标签: java spring-boot jpa

我正在映射三个实体。医生,客户(扩展人员)和医疗咨询。 参见上面的代码。考虑具有默认构造函数的所有模型类,具有所有字段以及getter和setter的构造函数:

a [(0, 0)]
b [(0, 1)]
c [(0, 2)]
x [(1, 0)]
y [(1, 1)]
z [(1, 2)]
k []

现在是全班医生。

@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); } 并根据超类及其自身的属性设置其值。 Client类也是如此。

super()

这里是医疗咨询模型,可以得到另外两个模型

@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;

最后,我们遇到了问题: 在我的控制器班上,我无法获得medicalConsultations的完整数据。也就是说,我得到了数据,ID和价格,但是由于某种原因我没有得到客户和医生。 但是,如果我调用方法@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; getDoctor()并返回其中之一,则可以看到所有信息。

请参阅RestControl类上的方法:

getClient

映射可能有问题。但是我将休眠设置为显示sql,这显然使所有查询都获得了我想要的一切。参见:

@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;
    }

有人可以告诉我什么吗?

1 个答案:

答案 0 :(得分:0)

尝试在MedicalConsultation上映射像波纹管这样的注释。

Optional<Set<Hobby>> hobbies;