Constant in Hibernate mapping files

I would like to add a value object to the mapped class, where one column is fixed depending on the class containing the component. How can I do something like this?

<component name="aComponent"> <property name="abc" column="cde"/> <property name="xyz" value="FIXED"/> </component> 

Unfortunately, the value attribute does not exist. Is there any other way to apply a constant value to a property?

Thanks in advance.

+6
hibernate
source share
1 answer

You should use a formula like

 <property name="xyz" formula="1" type="big_decimal"/> 

From Java Persistence with Hibernate, ch. 4.4.1:

This SQL formula is calculated every time an object is retrieved from the database (and not at any other time, so the result may be outdated if other properties are changed). The property does not have a column attribute (or subitem) and never appears in SQL INSERT or UPDATE , only in SELECT s. Formulas can refer to columns in a database table, they can call SQL functions, and they can even include SQL subqueries. The SQL expression is passed to the base database as is; this is a good chance to link your mapping file to a specific database product if you are careful and rely on vendor-specific operators or keywords.

+4
source share

All Articles