Java 从给定的实体类中截断/删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23269885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:35:35  来源:igfitidea点击:

truncate/delete from given the entity class

javajpacriteriacriteria-api

提问by xenoterracide

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.

我可以通过一种方法使用我的实体类。我想弄清楚,我如何通过 JPA JPQL 或 Criteria API 发出截断或删除。我认为标准 api 更适合处理类,而 truncate 是一种更快的操作,所以这些是首选。到目前为止,这是我整理的内容,但不确定要添加/更改什么。

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

note: _entityClassreturns MyEntity.class, I have no other references to MyEntitythis is a more generalized implementation.

注意:_entityClass返回MyEntity.class,我没有其他参考,MyEntity这是一个更通用的实现。

采纳答案by wypieprz

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

假设MyEntity引用您要删除的表,您可以按以下步骤操作:

// 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/SQL 查询:

// 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.

使用 Criteria API,您只能使用 SELECT、UPDATE、DELETE 语句,因此 TRUNCATE 是不可能的。