我正在尝试获取嵌套的对象属性,但是却出现了llegalArgument异常。
AuditTestingPlanSpecification name = new AuditTestingPlanSpecification(new SearchCriteria("auditPlanId.auditPlanEntity", ":",dates));
Page<AuditTestingPlanMaster> a = auditTestingPlanMasterRepository.findAll(name, ten);
请找到以下代码,
public class AuditTestingPlanSpecification implements Specification<AuditTestingPlanMaster> {
private SearchCriteria criteria;
public AuditTestingPlanSpecification(SearchCriteria searchCriteria) {
this.criteria = searchCriteria;
}
@Override
public Predicate toPredicate(Root<AuditTestingPlanMaster> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
if (root.get(criteria.getKey()).getJavaType() == String.class) {
return builder.like(root.<String>get(criteria.getKey()), "%" + criteria.getValue().get(0).toString() + "%");
} else {
return builder.equal(root.get(criteria.getKey()), criteria.getValue().get(0).toString());
}
return null;
}
}
SearchCriteria.java类
public class SearchCriteria {
public SearchCriteria(String key, String operation, List<Object> value) {
super();
this.key = key;
this.operation = operation;
this.value = value;
}
private String key;
private String operation;
private List<Object> value;
// getters & setters
}
AuditTestingPlanMaster.java类
@Entity
@Table(name = "audit_testing_plan_master")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class AuditTestingPlanMaster implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@Column(name = "risk_area_id")
private Long riskAreaId;
@Column(name = "expected_revert_date")
private Instant expectedRevertDate;
@Column(name = "created_date")
private Instant createdDate;
@Column(name = "last_modified_date")
private Instant lastModifiedDate;
@JoinColumn(name = "audit_plan_id", referencedColumnName = "id")
private AuditPlanMaster auditPlanId;
//getters & setters
}
AuditPlanMaster.java类
@Entity
@Table(name = "audit_plan_master")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class AuditPlanMaster implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@Column(name = "remarks", length = 255)
private String remarks;
@Column(name = "audit_plan_entity", length = 50)
private String auditPlanEntity;
@Column(name = "start_date")
private Instant startDate;
@Column(name = "end_date")
private Instant endDate;
//getters & setters
}
我想获取其AuditPlanMaster.auditPlanEntity字符串与提供的过滤器值匹配的所有AuditTestingPlanMaster对象。
感谢您的时间和事先的帮助。
答案 0 :(得分:2)
我遇到了同样的问题,这是我如何处理的摘要。我的问题是,当从内部usuario对象访问id字段时,在我的情况下,id就像您的auditPlanEntity,而usuario就像auditplanMaster:
public static Specification<UsuarioErrorEquipo> usuarioContains(String codigoUsuario) {
return (root, query, builder) -> {
Path<Usuario> u = root.get("usuario");
return builder.equal(u.get("id"), codigoUsuario);
};
}
我相信,在您的情况下,它应该类似于:
Path<AuditPlanMaster> u = root.get("auditPlanId");
return builder.equal(u.get("auditPlanEntity"), "the value you want to compare");