如何实现“全部删除”对于 Spring Roo 实体?
我正在尝试删除 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Transactional 不适用于静态函数
更改
为
https://jira.springsource.org/browse/SPR-5999
@Transactional doesn't work on static function
change
to
https://jira.springsource.org/browse/SPR-5999
Bye
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!