2
我使用JPA規範和CriteriaQuery中以where子句添加到我的實體..春天JPA使用上聯合表規格和CriteriaQuery中
域類:
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 =「abc」?
感謝您的幫助
工作!感謝隊友 – cgval
酷:)考慮使用元模型生成器,它是非常有用的工具 –
同意! :) 再次感謝 – cgval