Thirukumaran
Thirukumaran

Reputation: 367

Trigger a Event in Java

Requirement is to identify the modified attribute in the Entity Object.

How to identify the modified attribute in the request payload as a entity object when compare against the data of the previous value in the table.

Note : I don't want to compare each attribute against the table field,is there a way where i can compare the complete Entity object against table field.

Upvotes: 1

Views: 385

Answers (1)

so-random-dude
so-random-dude

Reputation: 16465

I don't want to compare each attribute against the table field

So you don't want boilerplate.

Then there are 3 options left.

  1. Reflection

Use Java Reflection to iterate (need Recursion if your model is not flat) through your member variables and compare. When I had this usecase I copied code from spring's beancopy and extended it.

  1. Use bytecode manipulation

    a. Runtime bytecode generation - use bytebuddy/cglib/asm to generate the boilerplate for you, when the app starts up

    b. Compiletime bytecode generation - Extend lombok to generate the boilerplate for you when your app compiles

  2. Don't use POJO, instead use Map<String,Object> You will get the flexibility of reflection but obviously, you will loose the benefit of a typed language here.

There is one more option, but this involves moving the event generation responsibility from app code to Database side.

  1. CDC (Change Data Capture)

If you use a database that supports change data propagation, you can decouple your application from this responsibility.

The catch is,

  1. This is an async operation

  2. the change data information depends on the database you choose. Old value may or may not be there. If old value is not there, then it again needs another strategy to figure out what got changed.

Upvotes: 1

Related Questions