【发布时间】:2021-06-18 20:34:30
【问题描述】:
我在使用谓词过滤第二个表时遇到问题。
目前为止:
Public class Account {
private long id;
private String name;
@OneToMany(mappedBy = "account", orphanRemoval = true, cascade = CascadeType.ALL)
private Post<List> post;
}
Public class Post {
private long id;
private Date datePost;
private String message;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "ACCOUNT_ID")
private Account account;
}
我的 dao 类看起来像:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<ExceptionDeltaEvent> cq = cb.createQuery(Account.class);
Root<Account> accountRoot = cq.from(Account.class);
List<Predicate> whereRestrictions = new ArrayList<>();
Join<Account, Post> accPost =
accountRoot.join("post", JoinType.INNER);
//2021-03-16 to 2021-03-20
whereRestrictions.add(cb.between(accPost.get("datePost"), fromDate, toDate));
cq.where(whereRestrictions.stream()
.toArray(Predicate[]::new))
.distinct(true);
TypedQuery<Account> query = em.createQuery(cq);
return query.getResultList();
这是我的 ACCOUNT 表:
| id | name |
|---|---|
| 1 | bob |
| 2 | ron |
POST 表:
| id | datepost | message | account_id |
|---|---|---|---|
| 1 | 2021-03-17 | First | 1 |
| 2 | 2021-03-19 | Second | 1 |
| 3 | 2021-03-28 | Hello | 1 |
| 4 | 2021-03-18 | Second | 2 |
并且返回数据应该是 acc id= 1 和 2,post id 为 1,2 和 4
但不知何故,它返回了所有的 POST 记录
【问题讨论】:
标签: java sql spring database spring-data-jpa