给定以下实体:
@Entity(name = "Object")
@Table(name = "OBJECT_TABLE")
public class ObjectEntity {
@EmbeddedId
private ObjectEntityPk id;
@Column(name = "SOME_ATTIBUTE")
private String someAttribute;
... Getters/Setters
}
以及以下主键对象:
@Embeddable
public class ObjectEntityPk {
@Column(name = "id1")
private String id1;
@Column(name = "id2)
private Long id2;
... Getters/Setters
}
我有 50 个 ObjectEntity 要删除,我想避免循环并执行 50 个删除请求。因此,我在我的 dao 中编写了以下代码以生成删除查询,例如DELETE FROM OBJECT_TABLE WHERE (id1, id2) IN ((someId1, someId2), (someOtherId1, someOtherId2), ...)
public void deleteObjects(final List<ObjectEntity> objects) {
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaDelete<ObjectEntity> deleteCriteria = criteriaBuilder.createCriteriaDelete(ObjectEntity.class);
Root<ObjectEntity> rootObject= deleteCriteria.from(ObjectEntity.class);
Expression<ObjectEntityPk> expression = rootObject.get("id");
Predicate predicate = expression.in(objects.stream().map(ObjectEntity::getId).collect(Collectors.toList()));
deleteCriteria.where(predicate);
this.entityManager.createQuery(deleteCriteria).executeUpdate();
}
除了生成的查询有问题。表单是正确的,但它没有正确绑定列名。请参阅我得到的查询。
DELETE FROM OBJECT_TABLE WHERE ((?, ?) IN ((?, ?), (?, ?), (?, ?), (?, ?), ...))
bind => [null, null, 0d287cacc63ff01b5222bf0bb2b2c794, 8105793672, 222a1399065f41b10814d88690c32bcf, 8105793874, 0b0c2f8e73475759872eb97a96f942a3, 8105794177, ...]
我不明白为什么我的列名没有绑定。我是否错过了 Entity 或 Pk 上的某些内容?
提前致谢。
PS:生成的查询在 Oracle 数据库上执行
编辑->使用@Guenther解决方案解决
通过使用多个谓词构造相同的请求来解决该问题。
public void deleteObjects(final List<ObjectsEntity> objects) {
CriteriaDelete<ObjectEntity> deleteCriteria = this.entityManager.getCriteriaBuilder()
.createCriteriaDelete(ObjectEntity.class);
Root<ObjectEntity> rootObject = deleteCriteria.from(ObjectEntity.class);
List<Predicate> predicates = objects.stream().map(n -> generateAndPredicate(n, rootObject))
.collect(Collectors.toList());
Predicate predicate = this.entityManager.getCriteriaBuilder().or(predicates.toArray(new Predicate[predicates.size()]));
deleteCriteria.where(predicate);
this.entityManager.createQuery(deleteCriteria).executeUpdate();
}
private Predicate generateAndPredicate(final ObjectEntity object,
final Root<ObjectEntity> rootObject) {
Predicate id1Equal = this.entityManager.getCriteriaBuilder().equal(rootObject.get("id").get("id1"),
object.getId().getId1());
Predicate id2Equal = this.entityManager.getCriteriaBuilder().equal(rootObject.get("id").get("id2"),
object.getId().getId2());
return this.entityManager.getCriteriaBuilder().and(id1Equal, id2Equal);
}
最终请求看起来像DELETE FROM OBJECT_TABLE WHERE ((ID1 = someId1 AND ID2 = someId2) OR (ID1 = someOtherId1 AND ID2 = someOtherId2) OR ...)