具有路径导航的相关实体的 JPA 标准或条件
JPA Criteria OR condition on related entity with path navigation
我有以下实体结构
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);
但是上面产生了以下 where 条件,它使用 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
按照评论中的建议使用 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);
我有以下实体结构
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);
但是上面产生了以下 where 条件,它使用 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
按照评论中的建议使用 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);