为什么@OneToMany 映射中存在循环?
Why is there a loop in @OneToMany mapping?
我正在尝试使用 JPA 创建一个@OneToMany 数据库。有一个对象 Flight 和一个对象 Passenger。
代码:
@Entity
@Table(name = "passengers")
public class Passenger {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
private String email;
private String phoneNumber;
private String birthDate;
@ManyToOne(optional = false)
@JoinColumn(name = "flight_id")
private Flight flight;
public Passenger() {
}
public Passenger(String name, String surname, String email, String phoneNumber, String birthDate, Flight flight) {
super();
this.name = name;
this.surname = surname;
this.email = email;
this.phoneNumber = phoneNumber;
this.birthDate = birthDate;
this.flight = flight;
}
@Entity
@Table(name = "flights")
public class Flight {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "flight_id")
private long id;
private String departure;
private String destination;
private String date;
private int capacity;
private float price;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "flight", cascade = CascadeType.ALL)
private Set<Passenger> passengers;
public Flight() {
}
public Flight(String departure, String destination, String date, int capacity, float price) {
super();
this.departure = departure;
this.destination = destination;
this.date = date;
this.capacity = capacity;
this.price = price;
}
这是我添加新乘客的方式:
@PostMapping("/flights")
public ResponseEntity<Object> updateFlight(@RequestBody Flight flight) {
long id = flight.getId();
Optional<Flight> flightOptional = flightRepository.findById(id);
if (!flightOptional.isPresent())
return ResponseEntity.notFound().build();
int currentCapacity = flight.getCapacity();
flight.setCapacity(currentCapacity - 1);
for(Passenger passenger : flight.getPassengers()) {
System.out.println(passenger.getName());
}
this.flightRepository.save(flight);
return ResponseEntity.noContent().build();
}
不幸的是,当我绘制航班和乘客地图时,我似乎有一个永无止境的循环。乘客有航班和乘客的详细信息,然后是航班,然后是乘客,等等。
有什么办法可以解决吗?我错过了什么吗?
为了避免循环问题使用@JsonManagedReference, @JsonBackReference 如下。
在 Parent class
上添加 @JsonManagedReference
@JsonManagedReference
@OneToMany(fetch = FetchType.EAGER, mappedBy = "flight", c
ascadee = CascadeType.ALL)
private Set<Passenger> passengers;
在 child class 上添加 @JsonBackReference,如下所示
@JsonBackReference
@ManyToOne(optional = false)
@JoinColumn(name = "flight_id")
private Flight flight;
我正在尝试使用 JPA 创建一个@OneToMany 数据库。有一个对象 Flight 和一个对象 Passenger。
代码:
@Entity
@Table(name = "passengers")
public class Passenger {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
private String email;
private String phoneNumber;
private String birthDate;
@ManyToOne(optional = false)
@JoinColumn(name = "flight_id")
private Flight flight;
public Passenger() {
}
public Passenger(String name, String surname, String email, String phoneNumber, String birthDate, Flight flight) {
super();
this.name = name;
this.surname = surname;
this.email = email;
this.phoneNumber = phoneNumber;
this.birthDate = birthDate;
this.flight = flight;
}
@Entity
@Table(name = "flights")
public class Flight {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "flight_id")
private long id;
private String departure;
private String destination;
private String date;
private int capacity;
private float price;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "flight", cascade = CascadeType.ALL)
private Set<Passenger> passengers;
public Flight() {
}
public Flight(String departure, String destination, String date, int capacity, float price) {
super();
this.departure = departure;
this.destination = destination;
this.date = date;
this.capacity = capacity;
this.price = price;
}
这是我添加新乘客的方式:
@PostMapping("/flights")
public ResponseEntity<Object> updateFlight(@RequestBody Flight flight) {
long id = flight.getId();
Optional<Flight> flightOptional = flightRepository.findById(id);
if (!flightOptional.isPresent())
return ResponseEntity.notFound().build();
int currentCapacity = flight.getCapacity();
flight.setCapacity(currentCapacity - 1);
for(Passenger passenger : flight.getPassengers()) {
System.out.println(passenger.getName());
}
this.flightRepository.save(flight);
return ResponseEntity.noContent().build();
}
不幸的是,当我绘制航班和乘客地图时,我似乎有一个永无止境的循环。乘客有航班和乘客的详细信息,然后是航班,然后是乘客,等等。
有什么办法可以解决吗?我错过了什么吗?
为了避免循环问题使用@JsonManagedReference, @JsonBackReference 如下。
在 Parent class
上添加 @JsonManagedReference@JsonManagedReference
@OneToMany(fetch = FetchType.EAGER, mappedBy = "flight", c
ascadee = CascadeType.ALL)
private Set<Passenger> passengers;
在 child class 上添加 @JsonBackReference,如下所示
@JsonBackReference
@ManyToOne(optional = false)
@JoinColumn(name = "flight_id")
private Flight flight;