I have the following entity and associated listener
@Entity
@EntityListeners(InjuryListener::class)
class Injury(val description: String,
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE) val id: Long = 0)
@Singleton
class InjuryListener : PreDeleteEventListener {
@PreRemove
fun preRemove(injury: Injury) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onPreDelete(event: PreDeleteEvent?): Boolean {
val injury = event?.entity as Injury
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
Yet when I delete an Injury neither of the the methods on my InjuryListener is called. Any clue about why that is?
As Ilya Dyoshin wrote @EntityListener
and @PreRemove
are not used in Micronaut Data. But you can solve it by AOP technique.
At first create your own interceptor with a pre-delete logic that you need:
@Singleton
class InjuryPreDeleteInterceptor : MethodInterceptor<Injury, Boolean> {
override fun intercept(context: MethodInvocationContext<Injury, Boolean>): Boolean {
val injury = context.parameterValues[0] as Injury
if (injury.someFlag) {
// do not delete
} else {
// delete
context.proceed()
}
return true
}
}
Then create annotation that will trigger the InjuryPreDeleteInterceptor
:
@MustBeDocumented
@Retention(RUNTIME)
@Target(AnnotationTarget.FUNCTION)
@Around
@Type(InjuryPreDeleteInterceptor::class)
annotation class VisitInjuryDelete
And add delete()
method signature annotated by previously created @VisitInjuryDelete
annotation into the InjuryRepository
interface:
@VisitInjuryDelete
override fun delete(Injury entity)
You can find more info about AOP in Micronaut here: https://docs.micronaut.io/latest/guide/aop.html