onetomany / manytoone 的无限循环?春天/休息api
Posted
技术标签:
【中文标题】onetomany / manytoone 的无限循环?春天/休息api【英文标题】:Infinite loop with onetomany / manytoone ? Spring / rest api 【发布时间】:2021-09-22 14:02:24 【问题描述】:我选择了从货架到仓库的多对一关系!我的问题是,当我将带有存储库的表的外键添加到带有存储库的表中时,我得到了一个带有数据的 ifnite 循环,我尝试使用 jsonignore / jsonmanagerefernce / jsonbackreference 来解决它,但是我的服务器 415 出现了问题.你们中有人帮我吗?我认为我的表的连接是罪魁祸首,但我不知道该怎么办..
实体仓库:
@Entity
@Table(name="warehouse")
//@Access(AccessType.FIELD)
public class Warehouse
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id_warehouse")
private Long id_warehouse;
@Column(name="description_warehouse")
private String description_warehouse;
@OneToMany(mappedBy = "war_id")
private List<Shelve> shelves = new ArrayList<>();
@OneToMany(mappedBy = "war_id")
private List<Management> managements = new ArrayList<>();
public Warehouse()
//getter setter
实体搁置:
@Entity
@Table(name = "shelve")
public class Shelve
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_shelve")
private Long id_shelve;
@Column(name = "description_shelve")
private String description_shelve;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "war_id", referencedColumnName = "id_warehouse")
private Warehouse war_id;
public Shelve()
//getter setter
错误:
Hibernate: select warehouse0_.id_warehouse as id_wareh1_2_0_, warehouse0_.description_warehouse as descript2_2_0_ from warehouse warehouse0_ where warehouse0_.id_warehouse=?
Hibernate: select shelves0_.war_id as war_id3_1_0_, shelves0_.id_shelve as id_shelv1_1_0_, shelves0_.id_shelve as id_shelv1_1_1_, shelves0_.description_shelve as descript2_1_1_, shelves0_.war_id as war_id3_1_1_ from shelve shelves0_ where shelves0_.war_id=?
2021-07-13 16:50:11.986 WARN 3968 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Failure while trying to resolve exception [org.springframework.http.converter.HttpMessageNotWritableException]
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:472) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.sendServerError(DefaultHandlerExceptionResolver.java:553) ~[spring-webmvc-5.3.8.jar:5.3.8]
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable(DefaultHandlerExceptionResolver.java:443) ~[spring-webmvc-5.3.8.jar:5.3.8]
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:210) ~[spring-webmvc-5.3.8.jar:5.3.8]
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:141) ~[spring-webmvc-5.3.8.jar:5.3.8]
at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:80) ~[spring-webmvc-5.3.8.jar:5.3.8]
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1323) ~[spring-webmvc-5.3.8.jar:5.3.8]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1134) ~[spring-webmvc-5.3.8.jar:5.3.8]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1080) ~[spring-webmvc-5.3.8.jar:5.3.8]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.8.jar:5.3.8]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.8.jar:5.3.8]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.8.jar:5.3.8]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.48.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.8.jar:5.3.8]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.48.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:228) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
【问题讨论】:
【参考方案1】:如果有人遇到同样的问题,请尝试输入 @JsonIgnore !并在您的应用程序属性中添加:spring.jackson.serialization.fail-on-empty-beans=false!
【讨论】:
【参考方案2】:您可以将@JsonManagedReference 放在Entity Warehouse 中,将@JsonBackReference 放在Entity Shelve 中。它可以很好地检索数据。
【讨论】:
【参考方案3】:你可以使用三个@JsonManagedReference @JsonBackReference @JsonIgnore 之一
【讨论】:
【参考方案4】:您可以在属性关系上使用@JsonIgnoreProperties
并输入属性名称
@JsonIgnoreProperties("shelves")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "war_id", referencedColumnName = "id_warehouse")
private Warehouse war_id;
@JsonIgnoreProperties("war_id")
@OneToMany(mappedBy = "war_id")
private List<Shelve> shelves = new ArrayList<>();
或者你可以使用@JsonManagedReference
换上@OneToMany
和@JsonBackReference
换上@ManyToOne
【讨论】:
以上是关于onetomany / manytoone 的无限循环?春天/休息api的主要内容,如果未能解决你的问题,请参考以下文章
如何正确映射@OneToMany 和@ManyToOne 关系,以便我可以保存和更新@OneToMany 端(有或没有@ManyToOne 端)