Skip to content

Add JpaSpecificationExecutor.delete(…) and ….deleteAll(…) [DATAJPA-907] #1262

Closed

Description

@spring-projects-issues

SPT Developer opened DATAJPA-907 and commented

I have to delete massive entities. And I cannot add deleteByXxxxx(...) to repository by some reason. So, I implemented deleteAll(Specification) myself for general-purpose. I think it would be nice that JpaSpecificationExecutor has deleteAll(Specification) interface.

Please consider this.

Example for SimpleJpaRepository (Worked with transaction)

private EntityManager entityManager;
private Class<E> entityClass;

public int deleteAll(Specification<E> spec) {
	
	CriteriaBuilder builder = entityManager.getCriteriaBuilder();
	CriteriaDelete<E> query = builder.createCriteriaDelete(entityClass);
	
	Predicate predicate = spec.toPredicate(query.from(entityClass), null, builder);
	query.where(predicate);
	
	return entityManager.createQuery(query).executeUpdate();
}

2 votes, 3 watchers

Activity

  1. spring-projects-issues commented on May 31, 2016

    @spring-projects-issues
    Author

    Oliver Drotbohm commented

    The reason that this is not implemented yet is that we're currently still JPA 2.0 compatible on the API level. ….createCriteriaDelete(…) was introduced in JPA 2.1. So with the introduction of the suggested method on JpaSpecificationExecutor we'd basically have to implement the method in a way that — if called on a JPA 2.0 EntityManager — would fail with a NoSucheMethodError. What also plays into this is that e.g. for OpenJPA, there's not even a JPA 2.1 compatible implementation available.

    I am a bit torn here. Spring Framework 4 still supports Hibernate 3 versions, so I am assuming that there's a decent user base on Hibernate versions < 4.3 (the one that introduced JPA 2.1 support). I'll take this forward to the team and see what we can do

  2. spring-projects-issues commented on May 31, 2016

    @spring-projects-issues
    Author

    SPT Developer commented

    I see. I expect future update. Thank you!

  3. s-jepsen commented on Mar 3, 2021

    @s-jepsen

    Any plans on implementing this anytime soon?

  4. self-assigned this
    on May 5, 2022
  5. added this to the 3.0 M4 (2022.0.0) milestone on May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions