JPA Criteria API Set 是否包含该值?
JPA Criteria API does Set contain the value?
想象一下图书馆。图书馆有书。每本书都是一个单独的实体。图书馆里有读者(人民)。当一个人拿一本书看的时候,我表示这本书是别人看过的。
问题的主题区已经重新设计,不要发誓逻辑
我不明白如何进行条件查询以查看 Set<People> peoples
是否包含某个人
的人
@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
我不知道如何编写仅检查 Id 的查询,但如果您有一个 People 实例,查询将如下所示
query.where(cb.isMember(peopleInstance, root.get("peoples"))
想象一下图书馆。图书馆有书。每本书都是一个单独的实体。图书馆里有读者(人民)。当一个人拿一本书看的时候,我表示这本书是别人看过的。
问题的主题区已经重新设计,不要发誓逻辑
我不明白如何进行条件查询以查看 Set<People> peoples
是否包含某个人
@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
我不知道如何编写仅检查 Id 的查询,但如果您有一个 People 实例,查询将如下所示
query.where(cb.isMember(peopleInstance, root.get("peoples"))