有三张表
book表:
id
title
author
bookCat表:
id
title
memo
book_Cat表:
book_id 外键
cat_id外键
book表:
id
title
author
bookCat表:
id
title
memo
book_Cat表:
book_id 外键
cat_id外键
@ManyToMany(cascade = CascadeType.PERSIST) @JoinTable(name = "book_cat", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "cat_id")) public Set<BookCat> getCats() { return cats; } //这个是book实体类中的多对多代码
问一下怎么通过cat_id查找到book表的内容?
/** * 创建动态查询条件组合 * * @param userId * @param searchParams * @return */ private Specification<Book> buildSpecification(Long catId, Map<String, Object> searchParams) { Map<String, SearchFilter> filters = SearchFilter.parse(searchParams); filters.put("user.id", new SearchFilter("user.id", Operator.EQ, catId)); Specification<Book> spec = DynamicSpecifications.bySearchFilter( filters.values(), Book.class); return spec; } //这个是本人看的例子 查询全部 本人不理解的是"user.id"是什么 假如本人改成cats就报错 有没有办法通过这个方法查询呢
在线等 急! 谢谢
解决方案
100
bookCat 实体类
@JoinTable(name = “book_Cat”, joinColumns = @JoinColumn(name = “cat_id”, nullable = false), inverseJoinColumns = @JoinColumn(name = “book_id “, nullable = false))
private List<book> books;
通过book_cat中间表关联
hql=”select b.title from book b left join on books where id=?”
hql里面全是实体名字 不是表名
@JoinTable(name = “book_Cat”, joinColumns = @JoinColumn(name = “cat_id”, nullable = false), inverseJoinColumns = @JoinColumn(name = “book_id “, nullable = false))
private List<book> books;
通过book_cat中间表关联
hql=”select b.title from book b left join on books where id=?”
hql里面全是实体名字 不是表名