1

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?

user672009
  • 4,379
  • 8
  • 44
  • 77

2 Answers2

0

As long as you are in micronaut world, majority of the "magic" is done during the compile-time. And one main statement of micronaut-data is that no runtime metamodel exist.

This effectively means, that there is no EntityListener annotation on your entity.

In micronaut world you should use DataInterceptor for implementing needed functionality.

Ilya Dyoshin
  • 4,459
  • 1
  • 19
  • 18
0

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

user672009
  • 4,379
  • 8
  • 44
  • 77
cgrim
  • 4,890
  • 1
  • 23
  • 42