JPA批量删除使用多列主键删除



给定以下实体:

@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个客观性要删除,我想避免循环并执行50个删除请求。因此,我在DAO中编写了以下代码以生成删除查询,例如从object_table中删除(id1,id2)in(((someID1,someid2),(someide2),(someotherid1,someotherid2),...) <</p>

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, ...]

我不明白为什么我的列名没有串成。我错过了实体或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);
} 

最终请求看起来像从object_table删除,其中((id1 = someID1和id2 = someID2)或(id1 = someotherid1 and id2 = someotherid2)或...)

生成的SQL是错误的。不可能绑定列名。这是由于以下事实,即JPA不支持在语句中的多个列。可以在此处找到更多详细信息的jpa查询,以供选择" in" in"中的哪个多个值条款

您需要创建id.id1 = x and id.id2 = y谓词的列表,并将or一起创建。

作为替代方案,您可以使用本机查询,如上所述答案中也提到。

相关内容

  • 没有找到相关文章