Calculated attribute loaded inside BeforeDetachEntityListener

Let us say I created a transient attribute “customerStatusDescription”.
This attribute can store the value “Open” or “Close” depending on the values in other attributes.

I know of two (2) approaches that can be used to load the attribute:

Approach #1:
One way is to define the “customerStatusDescription” attribute as a ReadOnly Transient attribute and then place logic inside the getter method in the Entity Class itself.

Approach #2:
Another method is to place the logic inside a onBeforeDetach Entity Listener method.

So my question is:
Although I generally prefer the first approach, what are the advantages and disadvantages of using approach#2 ?
In what cases, is it better to use approach #2 ?

#2 will be faster because dependent value is calculated only once.

But if base attributes are changing in the UI, e.g. in the entity editor form - dependent value won’t be automatically calculated. If you need such behavior, it’s better to use option #1.

1 Like