Liam
Liam

Reputation: 21

Detaching by JPA CriteriaDelete with in-expression having subquery seems not executing properly

I need to delete some entities with specific field being in an other query with help of CriteriaBuilder & CriteriaDelete. hare is what i have tries so far.

CriteriaDelete<Y> criteriaDelete = criteriaBuilder.createCriteriaDelete(orphan);


Root<Y> deleteRoot = criteriaDelete.from(one_type);
Root<T> queryRoot = criteriaDelete.from(other_type);

criteriaQuery.select(queryRoot.get(key));

entityManager.createQuery(criteriaDelete
        .where(deleteRoot.in(entityManager.createQuery(criteriaQuery
                .where(queryRoot.get("id").in(givenList))).getResultList())));

Which deleteRoot stands for entity that i want to remove and queryRoot can be any associated entity with in main entity, which we seek for having criteria delete with in expression from that, after executing method that contains the shared snippet, no data will affected.

Upvotes: 1

Views: 668

Answers (1)

Liam
Liam

Reputation: 21

With respects of properly adding predicates based on aliases and query components, once should explicitly call the executeUpdate method on javax.persistence.Query

entityManager.createQuery(criteriaDelete
    .where(deleteRoot.in(entityManager.createQuery(criteriaQuery
            .where(queryRoot.get("id").in(givenList))).getResultList())));

Some thing like

entityManager.createQuery(...).executeUpdate();

Upvotes: 1

Related Questions