How to handle ā€œcalculatedā€ fields in a Play Framework model

What are the ā€œbest practicesā€ for defining in the Play Framework objects that have ā€œcalculatedā€ fields and are not directly mapped to any of the fields associated with the entity?

For example, I would like to define a model for a ā€œproductā€ that has a ā€œlist priceā€ and a ā€œreal priceā€.

In DB terms, the ā€œlist priceā€ is directly mapped to the ā€œint listPriceā€ in the model class. However, the "real price" is calculated by the "list price" using additional data that is not related to the "product" itself (for example, the general discount of the store, the discount for specific departments, etc.), in which some "business logic" is involved )

Since my application needs to identify the REST API (and not just the web application), I would really like to extend the Product class to support the "calculated" field, therefore both "MyProduct.listPrice" and "MyProduct". finalPrice ".

Is it possible to add transition members to a model class? If not, should you determine the class derived from the model and use it?

Thanks for any hint.

Max

+4
source share
1 answer

Here is the way

@Entity public class Product extends Model { ... public int listPrice; @Transient int realPrice; public int getRealPrice() { return calcRealPrice(listPrice); } ... } 
+9
source

Source: https://habr.com/ru/post/1391928/


All Articles