我具有以下实体结构
public class Application {
@JoinColumn(name = "account_id")
@ManyToOne
private Account account;
@JoinColumn(name = "involved_account_id")
@ManyToOne
private Account involvedAccount;
}
public class Account {
private string id;
private string name;
}
我想获取account
名称或involvedAccount
名称与给定帐户名匹配的所有应用程序。
CriteriaQuery<Application> query = cb.createQuery(Application.class);
Root<Application> root = query.from(Application.class);
Predicate conditions = cb.conjunction();
conditions = cb.and(conditions, cb.or(
cb.like(
cb.upper(root.get("account").get("name")),
accountName.toUpperCase()
), cb.like(
cb.upper(root.get("involvedAccount").get("name")),
accountName.toUpperCase())
)
);
query.where(conditions);
query.select(root);
但是上面的代码在以下情况下产生了以下情况:将and
用作主键而不是or
where applicati0_.account_id=account1_.id
and applicati0_.involved_account_id=account2_.id
and 1=1
and (
upper(account1_.name) like ?
or upper(account2_.id) like ?
)
这是条件作为表达式失败的地方
applicati0_.account_id=account1_.id and applicati0_.involved_account_id=account2_.id
使用and
而不是or
答案 0 :(得分:0)
按照评论中的建议使用join
。
我目前无法测试,但是这样的方法应该可以工作:
CriteriaQuery<Application> query = cb.createQuery(Application.class);
Root<Application> root = query.from(Application.class);
Join<Application, Account> account = root.join("account");
Join<Application, Account> involvedAccount = root.join("involvedAccount");
Predicate conditions = cb.conjunction();
conditions = cb.and(conditions, cb.or(
cb.like(
cb.upper(account.get("name")),
accountName.toUpperCase()
), cb.like(
cb.upper(involvedAccount.get("name")),
accountName.toUpperCase())
)
);
query.where(conditions);
query.select(root);