What are the "best practices" to define - in Play Framework - objects that have some fields that are "calculated", rather than directly mapped to any of the fields related to an entity?
For example, I would like to define a Model for a "product" having a "list price" and "real price".
In terms of the DB, the "list price" is directly mapped to the "int listPrice" in the model class. However, the "real price" is calculated from the "list price" using additional data not related to the "product" itself (like, general store discount, department-specific discount etc. - some "business logic" involved).
Since my app needs to expose REST API (and not just a web app) - I would really want to extend the "Product" class to support the "calculated" field, so both "MyProduct.listPrice" and "MyProduct.finalPrice" will be supported.
Is it possible to add "transient" members to the model class? If not, should define a class derived from the model, and use it?
Thanks for any hint.
Max
Here is the way to go
@Entity public class Product extends Model {
...
public int listPrice;
@Transient int realPrice;
public int getRealPrice() {
return calcRealPrice(listPrice);
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With