Hi,
>>> I'm using annotations. I still can't get it working.
The posting has mapping files picked up directly from a working program. ( which I had set to mimic the scenario ).
I don't use ( neither I know much about ) Hibernate annonations.
>>> I don't understand the use of formulas for associations at all. The documentation keeps silence about it and the Address example (chapter 23) doesn't enlight me.
I'll put some light into their use in associations.
A) One formula. ( Simple Id )
Formula can be used when we want some expressions to link to the other end instead of plain column. Let's take our example,
<many-to-one class="Store" name="store" > <formula>decode(type, 'STORE',target_id, null)</formula> ( formula _1) </many-to-one> <many-to-one class="Product" name="product" > <formula>decode(type, 'PRODUCT',target_id, null)</formula> ( formula_2 ) </many-to-one>
Further query to fetch Store and Product would then be,
from Store where id = formula_1 ; from Product where id = formula_2 ;
If target_id = 25 and type = 'STORE', then, first formual evaluates to 25 and second to null. So, the association fetch queries become,
from Store where id = 25;
from Product where id = null ; ( We have realized the discrminator value )
B) two formula, back to back. ( Composite Id )
(Address Class ) <composite-id> <key-many-to-one name="person" column="personName"/> <key-property name="type" column="addressType"/> </composite-id> (Person Class) <one-to-one name="address" cascade="all"> <formula>name</formula> (formula_1) <formula>'HOME'</formula> (formula_2) </one-to-one>
Here, formula_1 maps to first key attribute, name.
formula_2 to second key attribute, addressType.
So,
Association fetching query is,
from Address where name = formula_1 and addressType = formula_2.
Note: You can use the tests from hibernate source distribution to get better understanding.
-----------------------------------------------------------
Rate the reply if you find it helpful
|