想象一個圖書館。圖書館藏書。每本書都是一個獨立的物體。圖書館里有讀者(人)。當一個人拿著一本書閱讀時,我表示這本書是被人閱讀的。
主題區域已針對問題重新設計,不要發誓邏輯
我無法理解如何進行 Criteria 查詢以查看是否Set<People> peoples包含某個 People 的人
@Entity
@Table(name = "book")
public class Book{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@Column
@NotEmpty
String title;
@ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(name = "people_read",
joinColumns = @JoinColumn(name = "id_people"),
inverseJoinColumns = @JoinColumn(name = "id_book"))
Set<People> peoples = new HashSet<>();
}
@Getter
@Setter
@Entity
@Table(name = "people")
public class People {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@Column
@NotEmpty
String name;
@ManyToMany(mappedBy = "peoples")
Set<Book> books = new HashSet<>();
}
我正在嘗試做的事情:
Path<Object> peoples = root.get("peoples");
(Predicate) query.where(peoples .get("id").in(cb.parameter(Set.class, "3"))); //3 is for example the id of people whom I want to know if they have read the book
uj5u.com熱心網友回復:
我不知道如何撰寫僅檢查 Id 的查詢,但如果您有一個 People 實體,查詢將如下所示
query.where(cb.isMember(peopleInstance, root.get("peoples"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/358242.html
