如何实现“全部删除”对于 Spring Roo 实体?

发布于 2024-11-01 05:27:25 字数 963 浏览 2 评论 0原文

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

public static int Licences.deleteAll() {
    return entityManager().createQuery("delete from Licences o").executeUpdate();
}

它编译得很好,但是当我调用 Licences.deleteAll() 我得到以下异常:

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

添加 @Transactional 没有什么区别。

我在这里缺少什么?

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

    public static void Licences.deleteAll() {
        for (Licences licence : findAllLicenceses()) {
            licence.remove();
        }
    }

这可行,但是 JPA 是否足够聪明,可以将其转换为 从许可证中删除 查询,还是会创建 n查询?

I'm trying to delete all database entries for a Spring Roo entity. When I look at *_Roo_Entity.aj it seems as if there is no "delete all" method. I tried to implement it myself (Licences is the name of the Roo entity. Don't mind the naming. It was reverese engineered from a database and may be changed later):

public static int Licences.deleteAll() {
    return entityManager().createQuery("delete from Licences o").executeUpdate();
}

It compiles just fine but when I call Licences.deleteAll() I get the following exception:

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

Adding @Transactional doesn't make a difference.

What am I missing here?

Is this approach completely wrong and I need to implement it like this:

    public static void Licences.deleteAll() {
        for (Licences licence : findAllLicenceses()) {
            licence.remove();
        }
    }

This works, but is JPA smart enough to translate this into a delete from licences query or will it create n queries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰魂雪魄 2024-11-08 05:27:25

@Transactional 不适用于静态函数

更改

public static int Licences.deleteAll() {
    return entityManager().createQuery("delete from Licences o").executeUpdate();
}

public int Licences.deleteAll() {
    return entityManager().createQuery("delete from Licences o").executeUpdate();
}

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

@Transactional doesn't work on static function

change

public static int Licences.deleteAll() {
    return entityManager().createQuery("delete from Licences o").executeUpdate();
}

to

public int Licences.deleteAll() {
    return entityManager().createQuery("delete from Licences o").executeUpdate();
}

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

Bye

淤浪 2024-11-08 05:27:25

JPA 没有删除所有功能。 (即使不使用 JQL!)

至少只有三种方法:

顺便说一句:看来您正在使用 AspectJ 来附加删除方法。 - 你可以这样做(即使我不知道,为什么不直接将静态方法添加到实体类中),但是你一定不要碰Roo生成的aj文件!

JPA does not have a delete all functionality. (even not with JQL!)

At least there are only three ways:

BTW: It seams that you are using AspectJ to attach you delete method. - You can do this (even if I do not know, why not adding the static method direct to the Entity class), but you must not touch the Roo generated aj files!

意中人 2024-11-08 05:27:25
public static int Licences.deleteAll() {
    return new Licences().deleteAllTransactional();
}

@Transactional
private int Licences.deleteAllTransactional() {
    if (this.entityManager == null) this.entityManager = entityManager();
    return this.entityManager.createQuery("delete from Licences o").executeUpdate();
}
public static int Licences.deleteAll() {
    return new Licences().deleteAllTransactional();
}

@Transactional
private int Licences.deleteAllTransactional() {
    if (this.entityManager == null) this.entityManager = entityManager();
    return this.entityManager.createQuery("delete from Licences o").executeUpdate();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文