I have a POJO managed by JPA that creates an external resource (via an HTTP message) inside a @PostConstruct
annotated method. When that Entity is deleted like so em.delete(instance)
I'd like for the cleanUp method to be called. I was thinking this could likely be done with an annotation provided by JPA but my search has turned up nothing. Below is an example entity.
@Entity
public class ExampleEntity {
// Constructors and Fields
@JpaAnnotation
public void cleanUp() {
// Performs clean up
}
// Methods
}
An additional example displaying how I'm using the cleanUp method.
@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);
}
}