gpt4 book ai didi

java - 多对多映射 JsonMappingException : Infinite recursion (StackOverflowError)

转载 作者:行者123 更新时间:2023-11-30 07:52:15 26 4
gpt4 key购买 nike

我知道这是一个重复的问题。这种类型的实现我做过很多次。不知道为什么这次不起作用。
非常标准的实现我知道这是一个重复的问题。这种类型的实现我做过很多次。不知道为什么这次不起作用。
非常标准的实现

public class Car implements BaseResponse {

@Id
@SequenceGenerator(name = "car_generator", sequenceName = "car_sequence", allocationSize = 1)
@GeneratedValue(generator = "car_generator")
private Long id;
@NotNull
private String name;
@NotNull
private String description;

@ManyToMany(fetch = FetchType.LAZY, mappedBy="cars")
private Set<part> parts;
}

@JsonIgnoreProperties({"cars"})
public class Part implements BaseResponse {

@Id
@SequenceGenerator(name = "part_generator", sequenceName = "part_sequence", allocationSize = 1)
@GeneratedValue(generator = "part_generator")
private Long id;

private String name;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "car_part",
joinColumns = @JoinColumn(name = "part_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "car_id", referencedColumnName = "id"))
private Set<car> cars;
}

create table if not exists parts
(
id BIGINT,
name varchar,
criteria varchar,
customers_count int,
primary key(id)
);

create table if not exists car_part
(
car_id bigint,
part_id bigint
);

我不断得到的错误是

Could not write content: Infinite recursion (StackOverflowError) (through reference chain: java.util.ArrayList[0]->com.saylent.domain.car["parts"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: java.util.ArrayList[0]->com.saylent.domain.car["parts"])

最佳答案

@JsonManagedReference@JsonBackReference旨在使用父字段和子字段之间的双向链接。

@ManyToMany(fetch = FetchType.LAZY, mappedBy="cars")
@JsonBackReference
private Set<part> parts;


@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "car_part",
joinColumns = @JoinColumn(name = "part_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "car_id", referencedColumnName = "id"))
@JsonManagedReference
private Set<car> cars;

关于java - 多对多映射 JsonMappingException : Infinite recursion (StackOverflowError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46121904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com