我正在尝试创建一个规范来根据他们的最新状态(可用、不可用)过滤人们。考虑以下实体。
public class Person{
@Id
private Long id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "person")
private Set<Status> statuses = new HashSet<>();
}
public class Status{
@Id
private Long id;
@Column(name = "created_at")
private Instant createdAt;
@ManyToOne
private StatusType statusType;
}
public class StatusType{
@Id
private Long id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "statusType")
private Set<Status> statuses = new HashSet<>();
}
在这个问题的帮助下,我创建了以下规范来根据他们的状态类型过滤一个人。
specification =
specification.and(
buildJoinSpecification(
criteria.getStatusTypeId(),
Person_.statuses,
Status_.statusType,
StatusType_.id
)
);
我知道我必须greatest
在createdAt
内部使用,Status
但我无法弄清楚语法。这是我的buildJoinSpecification
.
private <REFERENCE, JOIN, FILTER extends Comparable<? super FILTER>> Specification<Person> buildJoinSpecification(
RangeFilter<FILTER> filter,
SetAttribute<? super Person, REFERENCE> reference,
SingularAttribute<REFERENCE, JOIN> joinField,
SingularAttribute<JOIN, FILTER> valueField
) {
Specification<Person> result = Specification.where((Specification) null);
if (filter.getEquals() != null) {
result = this.equalsSpecification(reference, joinField, valueField, filter.getEquals());
}
if (filter.getIn() != null) {
result = this.valueIn(reference, joinField, valueField, filter.getIn());
}
return result;
}
private <REFERENCE, JOIN, FILTER> Specification<Person> equalsSpecification(
SetAttribute<? super Person, REFERENCE> reference,
SingularAttribute<REFERENCE, JOIN> joinField,
SingularAttribute<JOIN, FILTER> idField,
FILTER value
) {
return (root, query, builder) -> builder.equal(root.join(reference).join(joinField).get(idField), value);
}
private <REFERENCE, JOIN, FILTER> Specification<Person> valueIn(
SetAttribute<? super Person, REFERENCE> reference,
SingularAttribute<REFERENCE, JOIN> joinField,
SingularAttribute<JOIN, FILTER> valueField,
Collection<FILTER> values
) {
return (root, query, builder) -> {
CriteriaBuilder.In<FILTER> in = builder.in(root.join(reference).join(joinField).get(valueField));
for (FILTER value : values) {
in = in.value(value);
}
return in;
};
}
我将不胜感激任何帮助。谢谢。