Formula (attribute of property)

From JRapid

Jump to: navigation, search

formula is an attribute of property.



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.


<entity ... >
     name = NAME
     label = LABEL
     formula = SQL


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 = id"/>

    <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"/>

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" "">
<hibernate-mapping package="com.beta20101222.entities">
    <class name="Company" table="`company`" lazy="true">
        <id name="id" type="long">
            <generator class="native"/>
        <property name="name" column="`name`" type="string"/>
        <property name="employees" column="`employees`" type="string" 
               formula="(select count(*) from employee as e where = id)"/>

See also

Personal tools