我正在使用JPA规范和CriteriaQuery来向我的实体添加where子句..
域类:
public class Domain {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "account_id")
private Long accountId;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "domain_id")
private List<Notification> notification;
}
通知类:
public class Notification {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "domain_id")
private Long domainId;
@Column(name = "contact")
private String contact;
}
然后我有以下用于创建规范的类:
public class DomainSpecification implements Specification<Domain> {
final private SearchCriteria criteria;
public DomainSpecification(SearchCriteria searchCriteria) {
this.criteria = searchCriteria;
}
@Override
public Predicate toPredicate(
Root<Domain> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
switch (criteria.getOperation()) {
case ":":
if (root.get(criteria.getKey()).getJavaType() == String.class) {
return builder.like(
root.<String>get(criteria.getKey()),
"%" + criteria.getValue() + "%");
} else {
return builder.equal(root.get(criteria.getKey()),
criteria.getValue());
}
case "=":
return builder.equal(root.get(criteria.getKey()),
criteria.getValue());
default:
return null;
}
}
}
SearchCriteria对象:
public class SearchCriteria {
private final String key;
private final String operation;
private final Object value;
public SearchCriteria(String key, String operation, Object value) {
this.key = key;
this.operation = operation;
this.value = value;
}
public String getKey() {
return key;
}
public String getOperation() {
return operation;
}
public Object getValue() {
return value;
}
}
然后,每当我想创建一个where子句时,我都会执行以下操作:
DomainSpecification idSpecification
= new DomainSpecification(
new SearchCriteria("id", "=", domainId));
Specifications<Domain> specifications = Specifications.where(idSpecification);
final Domain domain = this.domainRepository.findOne(specifications);
这将通过域实体中名为id
的字段进行搜索。
现在,我如何通过我加入的实体进行过滤?例如,我想过滤Domain.id = 10和Notification.contact =&#34; abc&#34;?
感谢您的帮助
答案 0 :(得分:3)
您可以将Specification
的定义包装到帮助程序类中:
public class DelegationSpecificationsHelper {
public static Specification<Domain> notificationContactSpec(String contact) {
return (root, query, cb) -> cb.equal(root.join("notification").get("contact"), contact);
}
public static Specification<Domain> idSpec(SearchCriteria searchCriteria) {
switch (criteria.getOperation()) {
case ":":
if (root.get(criteria.getKey()).getJavaType() == String.class) {
return builder.like(
root.<String>get(criteria.getKey()),
"%" + criteria.getValue() + "%");
} else {
return builder.equal(root.get(criteria.getKey()),
criteria.getValue());
}
case "=":
return builder.equal(root.get(criteria.getKey()),
criteria.getValue());
default:
return null;
}
}
}
然后你可以像这样使用它:
Specifications<Domain> specifications = Specifications.where(DelegationSpecificationsHelper.idSpec(new SearchCriteria("id", "=", domainId))
.and(DelegationSpecificationsHelper.notificationContactSpec("someSearchString"));
静态导入和一些重构之后:
SearchCriteria idCriteria = new SearchCriteria("id", "=", domainId)
Specifications<Domain> specifications =
Specifications.where(idSpec(idCriteria)
.and(notificationContactSpec("someSearchString"));
当然你应该从这里摆脱硬编码值:
cb.equal(root.join("notification").get("contact"), contact);
并使用一些DTO对象或生成的JPA元模型。
添加元模型后,它可能如下所示:
public static Specification<Domain> notificationContactSpec(String contactValue) {
return (root, query, cb) -> cb.equal(root.join(Domain_.notification).get(Notification_.contact), contactValue);
}
有关元模型生成的更多信息: https://docs.jboss.org/hibernate/orm/5.0/topical/html/metamodelgen/MetamodelGenerator.html