Search code examples
openxava

In OpenXava Is it possible to have a calculated property that is also persisted in database?


OpenXava doc says:

A calculated property is a property that does not store its value in the database but it's calculated any time the property is accessed.

Is there a way to persist the calculated property?

The reason to do that would be:

  1. The calculation is so heavy/complex that executing it on demand at every access would be unfeasible;
  2. The calculation revolves around some random stuff that would not be the same every time the property is accessed.

Another way to put this question: Is there a way to store a property that was not entered by the user, but obtained from another source?


Solution

  • In OpenXava documenation a calculated property is a property with just a getter, with no attribute and setter and that is not stored in the table, but you have several ways of having properties with calculation and storing them in the table:

    1. Use @Formula annotation of Hibernate. Like explained here: https://openxava.org/OpenXavaDoc/docs/logic-from-database_en.html
    2. Use @Calculation annotation of OpenXava. @Calculation is for persistent properties. Look here: https://openxava.org/OpenXavaDoc/docs/calculation-and-collections-total_en.html
    3. Synchronize persistent and calculated property using JPA callback methods, as explained here: https://openxava.org/OpenXavaDoc/docs/synchronize-persistent-and-computed-properties_en.html