春季数据JPA:查询按列表查找对象吗?

发布于 2025-02-06 15:38:25 字数 724 浏览 2 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2025-02-13 15:38:26

我认为IN可以用平等代替。由于我们正在加入表,每行每一本书都将有一个相应的类别。因此,一旦加入它的滤波器,它的行滤波器具有我们要寻找的类别的行。

@Query("SELECT b FROM Book b join b.category c where c.category = :category")

我也认为,最好使用加入Fetch来摆脱绩效问题和所有问题。

@Query("SELECT b FROM Book b
join b.category c
join fetch b.category
 where c.category = :category")

我认为这有帮助!

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.

@Query("SELECT b FROM Book b join b.category c where c.category = :category")

Also I think its better to use Join fetch to get rid of performance issues and all.

@Query("SELECT b FROM Book b
join b.category c
join fetch b.category
 where c.category = :category")

I think this helps!!

梦晓ヶ微光ヅ倾城 2025-02-13 15:38:26

好吧,我犯了两个错误:

  1. 需要将fetch类型设置为渴望,或者有一个
    @OneToMany(fetch = FetchType.EAGER)
    private List<Category> category;

    @ManyToMany
    private List<Category> category;

  1. 从1而不是0的页面索引的Manytomany关系,从而为我提供了空的结果。
Pageable page = PageRequest.of(0, 1);
Page<Book> book = BookDao.getBooksByCat(page);

Well, I had made two mistakes:

  1. Needed to set fetch type to eager, or have a ManyToMany relationship
    @OneToMany(fetch = FetchType.EAGER)
    private List<Category> category;

    @ManyToMany
    private List<Category> category;

  1. I had page indexed from 1 instead of 0, thus giving me empty result.
Pageable page = PageRequest.of(0, 1);
Page<Book> book = BookDao.getBooksByCat(page);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文