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 prefered. 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.
Assuming that
MyEntity
refers to the table you want to drop you can proceed as follows:or with a generalized approach:
Similarly for JPQL/SQL queries:
or with a generalized approach:
With Criteria API you can only use SELECT, UPDATE, DELETE statements therefore TRUNCATE is not possible.