我的專案中有物體(基于Spring-Boot Hibernate):
@Entity
@Table(name = "user_account")
public class UserAccount {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@NotNull
@Column(name = "username")
private String userName;
@NotNull
@Column(name = "first_name")
private String firstName;
@NotNull
@Column(name = "last_name")
private String lastName;
@NotNull
@Column(name = "password")
private String password;
@CreationTimestamp
@Column(name = "birthday")
private LocalDateTime birthday;
@NotNull
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "role", referencedColumnName = "id")
private UserRole role;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userAccount", cascade= CascadeType.ALL)
private Set<RentInfo> rents;
}
和
@Entity
@Table(name = "rent_info")
public class RentInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@NotNull
@ManyToOne(cascade= CascadeType.ALL)
@JoinColumn(name="user_id")
private UserAccount userAccount;
@CreationTimestamp
@Column(name = "start_date")
private LocalDateTime startDate;
@CreationTimestamp
@Column(name = "end_date")
private LocalDateTime endDate;
@Column(name = "status")
private int status;
}
我想創建雙向一對多關系,當一個用戶可以有多個租金時,我們可以通過具體的 user_id 選擇租金,但是出了點問題
作為回應,我得到了這個:
{
"id": 1,
"userName": "[email protected]",
"firstName": "fName",
"lastName": "lName",
"password": "test",
"birthday": "2001-11-03T14:28:14",
"role": {
"name": "CLIENT"
},
"rents": [
{
"userAccount": {
"id": 1,
"userName": "[email protected]",
"firstName": "fName",
"lastName": "lName",
"password": "test",
"birthday": "2001-11-03T14:28:14",
"role": {
"name": "CLIENT"
},
"rents": [
{
"userAccount": {
"id": 1,
"userName": "[email protected]",
"firstName": "fName",
"lastName": "lName",
"password": "test",
"birthday": "2001-11-03T14:28:14",
"role": {
"name": "CLIENT"
}
.....
這是無限的,邏輯上我有記憶體不足錯誤。我怎樣才能解決這個問題?我做錯了什么?
uj5u.com熱心網友回復:
您有兩種解決方案:
- 在 @ManyToOne 上使用 @JsonIgnore
- 不要序列化您的物體。改用 DTO 并在映射時注意避免回圈依賴
uj5u.com熱心網友回復:
您還可以使用@JsonManagedReference和@JsonBackReference來解決這個無限遞回問題,如下所示:
@Entity
@Table(name = "user_account")
public class UserAccount {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@NotNull
@Column(name = "username")
private String userName;
@NotNull
@Column(name = "first_name")
private String firstName;
@NotNull
@Column(name = "last_name")
private String lastName;
@NotNull
@Column(name = "password")
private String password;
@CreationTimestamp
@Column(name = "birthday")
private LocalDateTime birthday;
@NotNull
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "role", referencedColumnName = "id")
private UserRole role;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userAccount", cascade= CascadeType.ALL)
@JsonManagedReference
private Set<RentInfo> rents;
}
@Entity
@Table(name = "rent_info")
public class RentInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@NotNull
@ManyToOne(cascade= CascadeType.ALL)
@JoinColumn(name="user_id")
@JsonBackReference
private UserAccount userAccount;
@CreationTimestamp
@Column(name = "start_date")
private LocalDateTime startDate;
@CreationTimestamp
@Column(name = "end_date")
private LocalDateTime endDate;
@Column(name = "status")
private int status;
}
不過,我會首先將您的物體映射到 DTO。但是,你還不如需要使用@JsonManagedReference,并@JsonBackReference在這些新的DTO類,如果你想要的所有資料可用(避免這一無限遞回問題將是未映單向userAccount的RentInfoDto,但你可能不希望這樣,因為你也想序列UserAccountDto資料)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/346840.html
上一篇:如何在URL中使用^讀取查詢引數
