我的物體是這樣設定的:
客戶
@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
@OneToMany(mappedBy = "client")
@JsonManagedReference
private List<Project> projects;
}
專案
@Entity
@Table(name = "project")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
@ManyToOne(name= "client")
@JsonBackReference
private Client client;
@OneToMany(mappedBy = "project")
@JsonManagedReference
private List<Listing> listings;
}
清單
@Entity
@Table(name = "listing")
public class Listing{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
@ManyToOne(name= "project")
@JsonBackReference
private Project project;
}
目前,如果我通過 ID 呼叫特定客戶,那么我將獲得客戶、他們的專案以及該專案的串列。這可以正常作業。但是,當我通過 ID 呼叫專案時,我只會得到該專案的串列,我還想知道哪個客戶擁有該專案。同樣,當我通過 ID 呼叫串列時,我只收到具有該 ID 的串列,我還想要擁有該串列的專案以及擁有它的客戶,這可能嗎?謝謝你。
uj5u.com熱心網友回復:
我看不出你所擁有的有什么問題,看起來它應該可以作業。
但是,即使它確實有效,我也希望它效率低下,因為它將進行兩次單獨的選擇,一次用于專案,另一次用于客戶。
可能最好進行顯式連接,如:
@Query("select p from Project p join fetch p.client where p.id = ?1")
這應該會產生一個帶有連接的查詢。
哦,在您發布的代碼中,您將“client”拼錯為“cient”
uj5u.com熱心網友回復:
注意:
@JsonManagedReference是參考的前向部分——正常序列化的部分。 @JsonBackReference是參考的后面部分——它將從序列化中省略。序列化的 Item 物件不包含對 Client 物件的參考。
使用@JsonIdentityInfo
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Client
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Project
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Listing
更多資訊:https : //www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/408765.html
標籤:
上一篇:按折扣百分比對產品進行排序
