[英]Map JPA Embedded entity class id to Embeddable entity class id
[英]Select query with JPA Criteria API When Entity class has a child class (Embeddable id)
我是JPA的新手。
我在以下链接中找到了如何使用JPA标准API: 具有多个参数的JPA标准API
谓词的代码为:
Root<CustomerEntity > customerEntity = cq.from(CustomerEntity.class);
List<Predicate> predicates = new ArrayList<Predicate>();
//Adding predicates in case of parameter not being null
if (param1 != null) {
predicates.add(
qb.equal(customerEntity.get("fieldName"), param1));
}
但是我的客户实体类具有可嵌入的ID(复合主键)
在这种情况下如何使用JPA条件API
所有可嵌入的id对象值也是搜索(选择查询)所必需的吗?
您必须执行2个步骤:
在可嵌入对象上实现equals和hashcode
像普通一样创建谓词
predicates.add(
qb.equal(customerEntity.get("id"), embeddableId));
更新您不能在不填充其所有值的情况下按整个对象进行搜索。 要仅搜索一个属性,只需遍历路径并进行比较
predicates.add(
qb.equal(customerEntity.get("id").get("property1"), embeddableId.getProperty1()));
希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.