没有找到org.hibernate.proxy.pojo. bytebuddyinterceptor类的序列化器.<



我有以下2个模型类:

package com.example.demo.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "casefiles")
public class CaseFile {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name="description")
private String description;

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "patient_id")
private Patient patient;

public CaseFile() {}
public CaseFile(String description, Patient patient) {
super();
this.description = description;
this.patient = patient;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Patient getPatient() {
return patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
}

package com.example.demo.model;
import java.time.LocalDate;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "patients")
public class Patient {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name = "name")
private String name;

@Enumerated(EnumType.STRING)
@Column(name = "gender")
private Gender gender;

@Column(name = "birth_date")
private LocalDate birthDate;

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "patient")
private CaseFile casefile;

public Patient() {}
public Patient(String name, Gender gender, LocalDate birthDate) {
super();
this.name = name;
this.gender = gender;
this.birthDate = birthDate;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public LocalDate getBirthOfDate() {
return birthDate;
}
public void setBirthOfDate(LocalDate birthDate) {
this.birthDate = birthDate;
}   
}

在我的控制器中,当我试图通过id:

获取一个casefile时
@GetMapping("/casefiles/{id}")
public ResponseEntity<CaseFile> getCasefileById(@PathVariable Long id) {
CaseFile casefile = caseFileRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("No casefile found with the id " + id));
return ResponseEntity.ok(casefile);
}

我得到这个错误:没有发现序列化器类org.hibernate.proxy.pojo. bytebuddybytebuddyinterceptor和没有发现属性来创建BeanSerializer(为了避免异常,禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:com.example.demo.model.CaseFile["patient"]- " com.example.demo.model.Patient$HibernateProxy$I2MAWVYg["hibernateLazyInitializer"])

这是因为FetchType。对病人偷懒。对于延迟加载到单连接,Hibernate将把代理放入变量中。

您需要在序列化之前初始化关系,例如使用JOIN FETCH。

查看五种可能的方法来初始化关系:https://thorben-janssen.com/5-ways-to-initialize-lazy-relations-and-when-to-use-them/

我认为问题是你没有为casefile提供getter和setter。因此,您无法获取该值。

public LocalDate getCaseFile() {
return caseFile;
}
public void setCaseFile(CaseFile casefile) {
this.caseFile = caseFile;
}   

最新更新