Formula (attribute of property)
From JRapid
formula is an attribute of property.
Contents |
Description
The formula attribute specifies how the value for a property must be calculated.
Properties using the formula attribute are not persisted and do not generate a column in the database table. They are calculated using the specified formula every time they need to be shown. The formula is defined using a SQL query. This query does not allow parameters but allows access to the entity's properties (id, etc).
The formula attribute maps to the attribute with the same name in the Hibernate mapping file (aka HBM).
If an entity's instance is not persisted, the values for formula properties cannot be calculated.
Usage
<entity ... >
<property
name = NAME
label = LABEL
...
formula = SQL
/>
</entity>
Example
This example shows hot to calculate the number of employees of a company.
When a Company form is opened for a specific record or the company records are listed, the employees property is calculated by the formula and shows the number of employees working for that company.
<entity label="Company" menu="Menu" name="Company">
<property display="primary" label="Name" name="name"/>
<property name="employees" display="secondary" label="Employees"
formula="select count(*) from employee as e where e.company = id"/>
</entity>
<entity label="Employee" menu="Menu" name="Employee">
<property display="primary" label="First Name" name="firstName"/>
<property display="primary" label="Last Name" name="lastName"/>
<property display="secondary" entity="Company" label="Company" name="company"/>
</entity>
The generated hbm file for the company entity shows how the formula is used.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.beta20101222.entities">
<class name="Company" table="`company`" lazy="true">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="name" column="`name`" type="string"/>
<property name="employees" column="`employees`" type="string"
formula="(select count(*) from employee as e where e.company = id)"/>
</class>
</hibernate-mapping>
