Pages

Using derived properties

Using derived properties

The value of derived properties is evaluated an expression at runtime by using formula attribute of property element in xml mapping metadata.

<property name="itemAmount" formula="20*ITEM_AMOUNT" type="double"/>

Property does not have column attribute or sub-element, it never appear in INSERT and UPDATE SQL statement. it appears only SELECT statement.
formula may refer to the column of database table, it can call SQL function and include sub-selects queries.
Note: formula is evaluated everytime when a entity instence is fetched from the underlying database (result may be outdated if property is modified).

Generated and default property values

Let properties of a class has its value that is generated by the database when a row is inserted or updated for the first time. So hibernate application needs to be refresh object that has these properties for which the database generates these values.

Hibernate provides generated attribute for property element in xml mapping metadata.
When Hibernate executes a INSERT or UPDATE SQL query that has this generated properties , it immediatly executes a SELECT SQL query to refresh the entity object that retrieved the generated values from the database.

         <property name="createdDate" column="CREATED_DATE" generated="insert" update="false" insert="false"/>  

Note: there are three attribute values for generated attribute : always, insert, never . always value is used for both INSERT and UPDATE SQL statement.


If you set the insert="false" and update="false" in the property element, than this column will never include in the INSERT and UPDATE SQL statement. the property value will treat as readOnly.


3 comments: