12

I have my entity class available via a method. I'm trying to figure out, how via the JPA JPQL or Criteria API's I could issue a truncate or delete from. I think that the criteria API is more natural for working with classes, and truncate is a faster operation so these are preferred. This is what I put together so far, but not sure what to add/change about it.

CriteriaBuilder cb = this._em().getCriteriaBuilder();
cb.createQuery( _entityClass() ).from( _entityClass() );

note: _entityClass returns MyEntity.class, I have no other references to MyEntity this is a more generalized implementation.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
xenoterracide
  • 16,274
  • 24
  • 118
  • 243

1 Answers1

19

Assuming that MyEntity refers to the table you want to drop you can proceed as follows:

// Criteria API (JPA 2.1 and above)
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaDelete<MyEntity> query = builder.createCriteriaDelete(MyEntity.class);
query.from(MyEntity.class);
em.createQuery(query).executeUpdate();

or with a generalized approach:

public <T> int deleteAllEntities(Class<T> entityType) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaDelete<T> query = builder.createCriteriaDelete(entityType);
    query.from(entityType);
    return em.createQuery(query).executeUpdate();
}


Similarly for JPQL/SQL queries:

// JPQL
em.createQuery("DELETE FROM MyEntity e").executeUpdate();

// SQL
em.createNativeQuery("TRUNCATE TABLE MyEntity").executeUpdate();

or with a generalized approach:

public static <T> int deleteAllEntities(Class<T> entityType) {
    String query = new StringBuilder("DELETE FROM ")
                            .append(entityType.getSimpleName())
                            .append(" e")
                            .toString();
    return em.createQuery(query).executeUpdate();
}

public static <T> int truncateTable(Class<T> entityType) {
    String query = new StringBuilder("TRUNCATE TABLE ")
                            .append(entityType.getSimpleName())
                            .toString();        
    return em.createNativeQuery(query).executeUpdate();
}

With Criteria API you can only use SELECT, UPDATE, DELETE statements therefore TRUNCATE is not possible.

wypieprz
  • 7,981
  • 4
  • 43
  • 46
  • What do you mean by "_made to work from the class_"? Could you please elaborate the question? – wypieprz Apr 25 '14 at 08:16
  • I have a reference to `MyEntity.class` I don't have the string `"MyEntity"` in fact I mutated this so that `CriteriaDelete` is just a cast `(CriteriaDelete)` because it didn't like me further paramaterizing it with `ENTITY` – xenoterracide Apr 25 '14 at 16:10
  • You might try to use Java generics, thus you wouldn't need to cast and parametrize a given query, see the updated answer above... – wypieprz Apr 26 '14 at 19:51
  • it didn't seem to like my generics in this case, something about the extends in the generic – xenoterracide Apr 27 '14 at 17:10
  • 1
    CriteriaDelete is not a truncate operation.Please do not mess delete and truncate with each others.Especially,using oracle as a DB it can cause a really big performance issue using delete instead of truncate with big tables. – enes.acikoglu Dec 08 '17 at 07:49