[英]Infinite loop with onetomany / manytoone ? Spring / rest api
I chose to have a many-to-one relationship from the shelves to the warehouses!我选择了从货架到仓库的多对一关系! My problem is that when I add to the table with the shelves the foreign key of the table with the repositories then I get an ifnite loop with data, I tried to solve it with jsonignore \/ jsonmanagerefernce \/ jsonbackreference but then my server 415 has a problem.
我的问题是,当我将带有存储库的表的外键添加到带有存储库的表中时,我得到了一个带有数据的 ifnite 循环,我尝试使用 jsonignore \/ jsonmanagerefernce \/ jsonbackreference 来解决它,但是我的服务器 415 出现了问题. happen to any of you to help me?
你们中有人帮我吗? I think the connection of my tables is to blame but I do not know what to do ..
我认为我的桌子的连接是罪魁祸首,但我不知道该怎么办..
Entity warehouse:实体仓库:
@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
You have wrong connections, you need a total refactor and understand the main ideas of the relationship's please look the link bellow:你有错误的连接,你需要彻底重构并理解关系的主要思想,请看下面的链接:
https://dev.to/jhonifaber/hibernate-onetoone-onetomany-manytoone-and-manytomany-8ba https://dev.to/jhonifaber/hibernate-onetoone-onetomany-manytoone-and-manytomany-8ba
If anyone has the same issue try to put @JsonIgnore !如果有人遇到同样的问题,请尝试放置 @JsonIgnore ! and add at your application properties this: spring.jackson.serialization.fail-on-empty-beans=false !
并在您的应用程序属性中添加: spring.jackson.serialization.fail-on-empty-beans=false !
You can put @JsonManagedReference in Entity Warehouse and @JsonBackReference in Entity Shelve.您可以将@JsonManagedReference 放在实体仓库中,将@JsonBackReference 放在实体搁架中。 It will work fine for retrieving data.
它可以很好地检索数据。
你可以使用三个@JsonManagedReference @JsonBackReference @JsonIgnore 之一
you can use @JsonIgnoreProperties
on property relation and put property name您可以在属性关系上使用
@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<>();
or you can use @JsonManagedReference
put on @OneToMany
and @JsonBackReference
put on @ManyToOne
或者你可以使用
@JsonManagedReference
放在@OneToMany
, @JsonBackReference
放在@ManyToOne
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.