Home > Mobile >  No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor One to One map
No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor One to One map

Time:10-28

I have the following 2 model classes:

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

and

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

}

In my controller , when i try to get a casefile by 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);
    }

I get this error: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.example.demo.model.CaseFile["patient"]->com.example.demo.model.Patient$HibernateProxy$I2MAWVYg["hibernateLazyInitializer"])

CodePudding user response:

That's because of FetchType.LAZY on the Patient. On a lazy loaded to-one connection Hibernate will put a proxy into the variable.

You need to initialize the relation before serialization for example with a JOIN FETCH.

Check out five possibile ways to initialize the relationship: https://thorben-janssen.com/5-ways-to-initialize-lazy-relations-and-when-to-use-them/

CodePudding user response:

I think the problem was you didn't provide a getter and setter for casefile. Therefore, you are not able to fetch the value.

 public LocalDate getCaseFile() {
        return caseFile;
    }

    public void setCaseFile(CaseFile casefile) {
        this.caseFile = caseFile;
    }   
  • Related