在两个子实体上进行筛选的Spring引导规范



有一个名为Transaction的实体,它有两个名为ServiceTransactionOtherFeeTransaction的映射(@OneToOne(子实体。能够使用规范对所有字段进行筛选,但同时对子实体字段进行筛选。

交易实体:

public class Transaction implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "uuid")
private UUID id;
@Column(name = "txn_id")
private String txnId;
@Column(name = "account_no")
private String accountNo;
@Column(name = "date", nullable = false)
private Date date;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "service_id")
private ServiceTransaction serviceTransaction;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "other_id")
private OtherFeeTransaction otherFeeTransaction;
}

其他费用实体:

public class OtherFeeTransaction implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "uuid")
private UUID id;
@Column(name = "other_fee_id")
private UUID otherFeeId;
@Column(name = "type_data_id")
private String typeDataId;
}

服务实体:

public class ServiceTransaction implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "uuid")
private UUID id;
@Column(name = "parcel_id")
private String cadastreId;
@Column(name = "ba_unit_id")
private String titleId;
}

规范

Predicate service = cb.like(root.<ServiceTransaction>get("serviceTransaction").get("titleId"),
"%" + criteria.getTitleId() + "%");
Predicate other = cb.like(root.<OtherFeeTransaction>get("otherFeeTransaction").get("typeDataId"),
"%" + criteria.getTitleId() + "%");
//works if any one of the predicates is added, but doesn't work if both given like below
predicateList.add(service);
predicateList.add(other);
criteriaQuery.where(cb.and(predicateList.toArray(new Predicate[predicateList.size()])))
.getRestriction();

如果给定predicateserviceother中的任何一个,则能够得到结果,但如果给定两个predicates,则不能得到结果。

即使尝试使用or caluse,它也无法使用

我成功了,忘记在映射实体上使用LeftJoin

Predicate service = cb.like(root.join("serviceTransaction",JoinType.LEFT).get("cadastreId"),
"%" + criteria.getParcelId() + "%");
Predicate other = cb.like(root.join("otherFeeTransaction",JoinType.LEFT).get("typeDataId"),
"%" + criteria.getParcelId() + "%");
predicateList.add(cb.or(service,other));

相关内容

  • 没有找到相关文章

最新更新