用于过滤两个子实体的 Spring Boot 规范
Posted
技术标签:
【中文标题】用于过滤两个子实体的 Spring Boot 规范【英文标题】:Spring boot Specification to filter on two child entities 【发布时间】:2020-03-10 11:29:54 【问题描述】:存在名称为Transaction
的实体,它有两个名称为ServiceTransaction
和OtherFeeTransaction
的映射(@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();
如果predicate
的service
或other
中的任何一个被给出,则能够得到结果,但如果同时给出predicates
,则不能。
即使尝试使用 or caluse
它也不起作用
【问题讨论】:
请添加实体。 添加了实体 究竟是什么问题/ 我不知道你的标准是什么。但是您应该先尝试查询数据库。可能没有符合条件的数据。 另外,你用“criteria.getTitleId()”创建了两个谓词,对吗? 【参考方案1】:我成功了,忘记在映射的实体上使用 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));
【讨论】:
以上是关于用于过滤两个子实体的 Spring Boot 规范的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot制作个人博客-框架搭建(设计与规范,实体类构建)