我有以下两个模型类:
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”]-
我认为问题是你没有为案例文件提供一个getter和setter。因此,您无法获取该值。
public LocalDate getCaseFile() {
return caseFile;
}
public void setCaseFile(CaseFile casefile) {
this.caseFile = caseFile;
}
这是因为FetchType。对病人懒惰。在延迟加载到一个连接上,Hibernate将在变量中放置一个代理。
您需要在序列化之前初始化关系,例如使用JOIN FETCH。
查看初始化关系的五种可能方法:https://thorben-janssen.com/5-ways-to-initialize-lazy-relations-and-when-to-use-them/
当我试图导航到一个endpoint时,我得到以下错误 类型定义错误:[简单类型,类org.hibernate.proxy.pojo.ByteBuddy.ByteBuddyInterceptor];嵌套异常为com.fasterxml.jackson.databind.exc.InvalidDefinitionException:未找到类org.hibernate.proxy.pojo.ByteBu
问题内容: 我有一个用户表和一个具有一对一映射表的表,其中一个字段用于此关系,该字段存储相应用户的ID字段值。 如何为此关系编写hibernate文件? 更新 我的问题是用户的主键是,user_detail的外键是 我在Internet用户user_id中获得的所有示例均作为用户主键,与其他表中的外键相同 问题答案: 对于用户映射…。 用于UserDetail映射。
你好,我正在尝试将我的一个实体“角色”持久化,它通过连接表与我的另一个实体“用户”保持多对一关系。然而,每次我尝试在没有任何附加用户的情况下插入角色时,都会出现以下错误:org。冬眠PropertyValueException:not null属性引用空值或瞬时值。我的角色实体映射到用户: 以及我的用户角色: 引发异常的代码: 目标是能够保存角色,而无需创建用户实例。我已经尝试过的事情: @多通(
com.fasterxml.jackson.databind.jsonMappingException:未发现用于java.io.BufferedReader类的序列化程序,也未发现用于创建BeanSerializer的属性(为了避免异常,禁用SerializationFeature.fail_on_empty_beans)(通过引用链:pj.core.bean.actionResult[“dat
有来自web服务的JSON,JSON数组作为响应 在JsonArray中获取响应后,读取Json数组的Json对象时出错: 没有找到JSONObject类org.json.序列化程序,也没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationConfig. Feature.FAIL_ON_EMPTY_BEANS))