简体   繁体   English

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

[英]Spring boot : java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

I am getting this error because of @OneToOne.由于@OneToOne,我收到此错误。 Cannot call sendError() after the response has been committed Can someone help me figure how to resolve it?响应提交后无法调用 sendError() 谁能帮我弄清楚如何解决它?

Here are my models :这是我的模型:

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

I tried adding @JsonIgnore (this solution : Spring Boot : Error :Cannot call sendError() after the response has been committed ) but I get this error :我尝试添加@JsonIgnore(此解决方案: Spring Boot:错误:提交响应后无法调用sendError() )但我收到此错误:

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

I also tried removing the fetch type and it didn't work either.我也尝试删除 fetch 类型,但它也不起作用。

So the solution that worked for me is I used the @MapsId in one of the models and removed the field from the other class.所以对我有用的解决方案是我在其中一个模型中使用了 @MapsId 并从另一个类中删除了该字段。 With @MapsId you don't need a bidirectional association.使用@MapsId,您不需要双向关联。 For more details you can read this article : https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/有关更多详细信息,您可以阅读这篇文章: 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate和Jackson(java.lang.IllegalStateException:提交响应后无法调用sendError()) - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) Spring java.lang.IllegalStateException:在提交响应后无法创建会话 - Spring java.lang.IllegalStateException: Cannot create a session after the response has been committed java.lang.IllegalStateException:在提交响应后无法调用sendRedirect() - java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed java.lang.IllegalStateException:添加`return`后提交响应后无法转发 - java.lang.IllegalStateException: Cannot forward after response has been committed after add `return` java.lang.IllegalStateException:在404上提交响应后无法转发 - java.lang.IllegalStateException: Cannot forward after response has been committed on 404 java.lang.IllegalStateException - 在提交响应后无法创建会话 - java.lang.IllegalStateException - Cannot create a session after the response has been committed 随机java.lang.IllegalStateException:提交响应后无法转发 - Random java.lang.IllegalStateException: Cannot forward after response has been committed JSF 2.2 java.lang.IllegalStateException:提交响应后无法创建会话 - JSF 2.2 java.lang.IllegalStateException: Cannot create a session after the response has been committed java.lang.IllegalStateException:在servletfilter中提交响应后无法转发 - java.lang.IllegalStateException: Cannot forward after response has been committed in servletfilter java.lang.IllegalStateException:在提交响应后无法转发 - java.lang.IllegalStateException: Cannot forward after response has been committed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM