【发布时间】:2019-03-21 18:33:41
【问题描述】:
我有一个由 JPA 管理的 POJO,它在 @PostConstruct 带注释的方法中创建一个外部资源(通过 HTTP 消息)。当像em.delete(instance) 这样删除该实体时,我希望调用 cleanUp 方法。我在想这很可能可以通过 JPA 提供的注释来完成,但我的搜索一无所获。下面是一个示例实体。
@Entity
public class ExampleEntity {
// Constructors and Fields
@JpaAnnotation
public void cleanUp() {
// Performs clean up
}
// Methods
}
显示我如何使用 cleanUp 方法的附加示例。
@Entity
@Component
public class ExampleEntity {
// Id and managed columns
private String externalResourceId;
@Transient
private static CustomHttpService service;
// Constructors
@Autowired
public void setCustomHttpService(CustomHttpService service) {
ExampleEntity.service = service;
}
// Methods
@PostConstruct
public void createExternalResource() {
if (externalResourceId == null || externalResourceId.isEmpty()) {
externalResourceId = service.createExternalResource();
}
}
@JpaAnnotation
public void deleteExternalResource() { // This is my example of the cleanUp method
service.deleteExternalResource(externalResourceId);
}
}
【问题讨论】:
-
您能否提供一个场景,说明您尝试使用
cleanUp()的原因和目的 -
@Vishrant 我添加了我的用例作为示例
标签: java spring-boot jpa spring-data-jpa