【问题标题】:Spring boot : java.lang.IllegalStateException: Cannot call sendError() after the response has been committedSpring boot:java.lang.IllegalStateException:响应提交后无法调用 sendError()
【发布时间】:2020-02-09 11:35:31
【问题描述】:

由于@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

我也尝试删除 fetch 类型,但它也不起作用。

【问题讨论】:

    标签: java spring


    【解决方案1】:

    所以对我有用的解决方案是我在其中一个模型中使用了@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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2018-12-06
      相关资源
      最近更新 更多