Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant in Hibernate Mapping Files

Tags:

hibernate

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

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

Unfortunatly, the value attribute does not exist. Is there another way to apply a constant value to property?

Thanks in advance.

like image 603
bertolami Avatar asked Jun 18 '10 10:06

bertolami


People also ask

What we are writing in hibernate mapping file?

This mapping file instructs Hibernate — how to map the defined class or classes to the database tables? Though many Hibernate users choose to write the XML by hand, but a number of tools exist to generate the mapping document. These include XDoclet, Middlegen and AndroMDA for the advanced Hibernate users.

Which of the following element is used to map many to many relationship in hibernate?

Define Hibernate Mapping File The <set> element will be used to define the rule for manyto-many relationship. The mapping document is an XML document having <hibernate-mapping> as the root element which contains two <class> elements corresponding to each class.

What is many to many mapping in hibernate?

Hibernate. Hibernate Mapping. Learn to create and manage many-to-many relationships between entities in a hibernate/JPA-based applications using @ManyToMany annotation. A many-to-many association is made between two entities where one entity can be associated with multiple other instances of the other entity.


1 Answers

You should use a formula, e.g.

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

From Java Persistence with Hibernate, ch. 4.4.1:

The given SQL formula is evaluated every time the entity is retrieved from the database (and not at any other time, so the result may be outdated if other properties are modified). The property doesn’t have a column attribute (or subelement) and never appears in an SQL INSERT or UPDATE, only in SELECTs. Formulas may refer to columns of the database table, they can call SQL functions, and they may even include SQL subselects. The SQL expression is passed to the underlying database as is; this is a good chance to bind your mapping file to a particular database product, if you aren’t careful and rely on vendor-specific operators or keywords.

like image 181
Péter Török Avatar answered Sep 23 '22 13:09

Péter Török