【发布时间】:2019-08-06 00:57:22
【问题描述】:
假设我的 Java、spring boot、hibernate 应用程序中有这个类:
@Entity
@Table(name="person")
@Getter @Setter @NoArgsConstructor
public class Person{
@Id
@Column(name="ID", nullable=false)
private int Id;
@Column(name="PERSON_ID")
private String personId;
@Column(name="FIRST_NME")
private String firstName;
@Column(name="LAST_NME")
private String lastName;
@OneToMany(fetch=FetchType.LAZY, mappedBy = "person", cascade = {CascadeType.ALL})
private List<Award> awards;
}
假设这是奖励课程:
@Entity
@Table(name="award")
@Getter @Setter @NoArgsConstructor
public class Award{
@Id
@Column(name="COMPOSITE_ID", nullable=false)
private int Id;
@Column(name="AWARD_CODE")
private String awardCode;
@Column(name="AWARD_NAME")
private String awardName;
@ManyToOne
@JoinColumn(name="PERSON_ID")
private Personn person;
}
当我像这样从我的 jpa 存储库中简单地查找所有内容时:
List<Person> findAll();
我得到这个错误:
failed to lazily initialize a collection of role: com.my.proj.datastores.legacy.model.Person.awards, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.my.proj.datastores.legacy.model.Person.awards, could not initialize proxy - no Session (through reference chain: com.my.proj.datastores.ngl.model.Person["awards"])]
不完全确定为什么...如果我执行@JsonIgnore,我可以使请求工作,但我想要那些奖励子对象。
【问题讨论】:
标签: java hibernate spring-boot