如何为Spring Roo实体实现"全部删除"?

hvo*_*mer 3 jpa transactional spring-roo

我正在尝试删除Spring Roo实体的所有数据库条目.当我看到*_Roo_Entity.aj它似乎没有"全部删除"方法.我试图自己实现它(Licences是Roo实体的名称.不要介意命名.它是从数据库设计的,可能会在以后更改):

public static int Licences.deleteAll() {
    return entityManager().createQuery("delete from Licences o").executeUpdate();
}
Run Code Online (Sandbox Code Playgroud)

它编译得很好但是当我打电话时Licences.deleteAll()我得到以下异常:

org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; 
nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query (NativeException)
Run Code Online (Sandbox Code Playgroud)

添加@Transactional并没有什么区别.

我在这里错过了什么?

这种方法是完全错误的,我需要像这样实现它:

    public static void Licences.deleteAll() {
        for (Licences licence : findAllLicenceses()) {
            licence.remove();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是有效的,但JPA是否足够智能delete from licences将其转换为查询或是否会创建n查询?

小智 6

@Transactional不适用于静态函数

更改

public static int Licences.deleteAll() {
    return entityManager().createQuery("delete from Licences o").executeUpdate();
}
Run Code Online (Sandbox Code Playgroud)

public int Licences.deleteAll() {
    return entityManager().createQuery("delete from Licences o").executeUpdate();
}
Run Code Online (Sandbox Code Playgroud)

https://jira.springsource.org/browse/SPR-5999

再见