Spring Boot:java.lang.IllegalStateException:提交响应后无法调用sendError()

时间:2020-02-09 11:35:31

标签: java spring

由于@OneToOne,我收到此错误。提交响应后无法调用sendError()有人可以帮助我确定如何解决该问题吗?

这是我的模特:

@Entity
@SequenceGenerator(name = "RECOMMENDATION_SQ", sequenceName = "recommendation_sequence")
public class Review {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "RECOMMENDATION_SQ")
private Long id;

@ManyToOne
private Restaurant restaurant;

@ManyToOne
private User user;

private Date date;

@Lob
private byte[] image;

private String text;

@OneToOne(fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinColumn(name="id_rating")
private Rating rating;

-

@Entity
@SequenceGenerator(name = "RATING_SQ", sequenceName = "rating_sequence")
public class Rating {

@Id
@GeneratedValue
private Long id_rating;

@OneToOne(fetch=FetchType.LAZY, cascade =  CascadeType.ALL, mappedBy = "rating")
@JsonIgnore
private Review review;

private int dish;
private int service;
private int price;
private int location;
private int accessibility;

我尝试添加@JsonIgnore(此解决方案:Spring Boot : Error :Cannot call sendError() after the response has been committed),但出现此错误:

InvalidDefinitionException: No serializer found for class 
org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create 
BeanSerializer

我还尝试了删除访存类型,但是它也不起作用。

1 个答案:

答案 0 :(得分:0)

所以对我有用的解决方案是我在其中一个模型中使用了@MapsId,并从另一个类中删除了该字段。 使用@MapsId,您不需要双向关联。 有关更多详细信息,请阅读本文:https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/

@Entity
@SequenceGenerator(name = "RECOMMENDATION_SQ", sequenceName = 
"recommendation_sequence")
public class Review {

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "RECOMMENDATION_SQ")
private Long id;

@ManyToOne
private Restaurant restaurant;

@ManyToOne
private User user;

private Date date;

@Lob
private byte[] image;

private String text;

@OneToOne(fetch = FetchType.LAZY)
@MapsId
@JoinColumn(name = "id")
private Rating rating;

-

@Entity
@SequenceGenerator(name = "RATING_SQ", sequenceName = "rating_sequence")
public class Rating {

@Id
@GeneratedValue
private Long id;

private int dish;
private int service;
private int price;
private int location;
private int accessibility;