Hibernate 3 Formulas
one-to-one
In Hibernate, one-to-one is mainly used for two
tables sharing the same primary key. For foreign key association,
we normally use many-to-one instead. However, with
formula, one-to-one is able to link up
tables via a foreign key. The above many-to-one
example can be mapped by one-to-one as follows:
<hibernate-mapping>
<class name="Company" table="Company" >
<id name="id" />
<one-to-one name="defaultContactPerson"
property-ref="defaultContactPerson" >
<formula>id</formula>
<formula>1</formula>
</many-to-one>
</class>
<class name="Person" >
<id name="id" />
<properties name="defaultContactPerson">
<property name="companyID" />
<property name="defaultFlag" />
</properties>
</class>
</hibernate-mapping>
Others: many-to-many
formula can be used with a
many-to-many element for a special joining from
relationship table to an entity table, although this is not often
needed.
Summary
The examples in this article show most of the
formula usage scenarios. When a formula
evaluation value is needed, the formula expression
will appear in the select part of the resulting SQL
statement. And when a formula is used for joining, it
appears in the where part of the resulting SQL statement.
Furthermore, a formula expression can use any dialect
of SQL, as long as the target database can support it. As a result,
formula helps achieve fine-grained mapping from data
model to object model without code.
Resources
- Sample code for this article.
- Hibernate provides most
information about Hibernate ORM.
-
Filter Article on TheServerSide for external filter
introduction.
- Spring is a very
friendly framework and provides integration with Hibernate and
other data access tools.
- "Dependency
Injection:" A definitive introduction of dependency
injection.
- Dependency
Injection Basics: An easy-to-understand introduction of
dependency injection.
Dai Yifan
is a technical consultant for a leading banking solution provider
Return to ONJava.com.
Prev [1] [2] [3] [4]