春季数据JPA:查询按列表查找对象吗?
我有2个课程:具有OneTomany关系的书籍和类别。 我想通过包含类别的字符串列表。结果应返回与列表中任何类别相匹配的所有书籍。例如,如果一本书有5个类别,并且在列表中仅匹配1个类别,则仍应将其返回。
这就是我想到的,但它不起作用。
//Models:
@Entity
@Table(name = "Book")
@Data
public class Book {
@Id
private String id;
@OneToMany
private List<Category> category;
}
@Entity
@Table(name="Category")
@Data
public class Category {
@Id
private int id;
private String category;
}
//
@Repository
public interface BookDao extends JpaRepository<Book, Integer> {
@Query("SELECT b FROM Book b join b.category c where c.category in :category")
Page<Book> getBooksByCat(Pageable pageable, @Param("category") List<String> category);
}
I have 2 classes: book and category which have a OneToMany relationship.
I want to pass a list of string containing categories. The result should return all books which match any of the category in the list. for example if a book has 5 category and it matches with only 1 on the list, it should still be returned.
This is what I came up with but its not working.
//Models:
@Entity
@Table(name = "Book")
@Data
public class Book {
@Id
private String id;
@OneToMany
private List<Category> category;
}
@Entity
@Table(name="Category")
@Data
public class Category {
@Id
private int id;
private String category;
}
//
@Repository
public interface BookDao extends JpaRepository<Book, Integer> {
@Query("SELECT b FROM Book b join b.category c where c.category in :category")
Page<Book> getBooksByCat(Pageable pageable, @Param("category") List<String> category);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为IN可以用平等代替。由于我们正在加入表,每行每一本书都将有一个相应的类别。因此,一旦加入它的滤波器,它的行滤波器具有我们要寻找的类别的行。
我也认为,最好使用加入Fetch来摆脱绩效问题和所有问题。
我认为这有帮助!
I think the in can be replaced with equals. Since we are joining the tables right each row of book will have a corresponding category. So once joined its just filter those rows which has the category that we are looking for.
Also I think its better to use Join fetch to get rid of performance issues and all.
I think this helps!!
好吧,我犯了两个错误:
Well, I had made two mistakes: