gpt4 book ai didi

java - 在联合表上使用 Specifications 和 CriteriaQuery 的 Spring JPA

转载 作者:行者123 更新时间:2023-11-29 01:51:51 26 4
gpt4 key购买 nike

我正在使用 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 的字段进行搜索。

现在,我还可以如何按我要加入的实体进行过滤?例如,我想过滤 where Domain.id = 10 and Notification.contact = "abc"?

谢谢你的帮助

最佳答案

您可以将您的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

关于java - 在联合表上使用 Specifications 和 CriteriaQuery 的 Spring JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39614947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com