gpt4 book ai didi

java - JPA/Spring Roo : is there a destructor event called when an entity is removed from the persistent store?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:14 25 4
gpt4 key购买 nike

我有一个图像实体 (Spring Roo/JPA),它引用存储在文件系统上的图像。当图像实体从持久存储中删除时,我很乐意删除这些文件。

我可以在每个 Image 实体上编写手动 remove() 方法,使其删除自身及其引用的文件,但这仅在使用该方法手动删除图像时才有效。当拥有的实体被移除时,或者通过调用 entityManager.remove() 将图像级联移除时,它显然不会触发。

当从数据库中删除实体时,是否会调用任何类型的方法或事件?

最佳答案

是的。通常,这些方法称为 lifecycle callback methods .在您的情况下,您需要删除后回调方法。这可以位于单独的类(然后通过 @EntityListeners 注释引用),或直接位于您的实体:

@PostRemove
protected void postRemoveOperations() {
//remove images here
}

或视详情而定,@PreRemove可以更有用。

关于java - JPA/Spring Roo : is there a destructor event called when an entity is removed from the persistent store?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10320664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com