简体   繁体   中英

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

I am getting this error because of @OneToOne. Cannot call sendError() after the response has been committed Can someone help me figure how to resolve it?

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 :

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.

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. With @MapsId you don't need a bidirectional association. For more details you can read this article : 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM