ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Hibernate 3 Formulas
Pages: 1, 2, 3, 4


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:

  <class name="Company" table="Company" >
    <id name="id" />
    <one-to-one name="defaultContactPerson" 
        property-ref="defaultContactPerson" >
  <class name="Person" >
    <id name="id" />
    <properties name="defaultContactPerson">
      <property name="companyID" />
      <property name="defaultFlag" />

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.


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.


Dai Yifan is a technical consultant for a leading banking solution provider

Return to ONJava.com.