我有以下 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 獲取案例檔案時:
@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.bytebuddy.ByteBuddyInterceptor 的序列化程式,也沒有發現創建 BeanSerializer 的屬性(為了避免例外,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通過參考鏈:com.example.demo.model .CaseFile["患者"]->com.example.demo.model.Patient$HibernateProxy$I2MAWVYg["hibernateLazyInitializer"])
uj5u.com熱心網友回復:
那是因為患者上的 FetchType.LAZY。在延遲加載的一對一連接上,Hibernate 會將代理放入變數中。
您需要在序列化之前初始化關系,例如使用 JOIN FETCH。
查看五種可能的初始化關系的方法:https : //thorben-janssen.com/5-ways-to-initialize-lazy-relations-and-when-to-use-them/
uj5u.com熱心網友回復:
我認為問題在于您沒有為 casefile 提供 getter 和 setter。因此,您無法獲取該值。
public LocalDate getCaseFile() {
return caseFile;
}
public void setCaseFile(CaseFile casefile) {
this.caseFile = caseFile;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/339856.html
上一篇:SpringJPA測驗-回滾
