当前位置: 首页 > 知识库问答 >
问题:

找不到类org的序列化程序。冬眠代理波乔。拜特巴迪。ByteBuddyInterceptor一对一映射Hibernate

丁灿
2023-03-14

我有以下两个模型类:

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的序列化程序。冬眠代理波乔。拜特巴迪。ByteBuddyInterceptor,未发现创建BeanSerializer的属性(为了避免异常,请禁用SerializationFeature.FAIL\u ON\u EMPTY\u bean)(通过引用链:com.example.demo.model.CaseFile[“patient”]-

共有2个答案

蔡修远
2023-03-14

我认为问题是你没有为案例文件提供一个getter和setter。因此,您无法获取该值。

 public LocalDate getCaseFile() {
        return caseFile;
    }

    public void setCaseFile(CaseFile casefile) {
        this.caseFile = caseFile;
    }   
聂华翰
2023-03-14

这是因为FetchType。对病人懒惰。在延迟加载到一个连接上,Hibernate将在变量中放置一个代理。

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

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

 类似资料: