moonlight wrote:
Let's say I have 2 classes: Order and Items. Each class Order has many class Items. So it's a one to many association, right?
So, to implement it... I must create a property like "orderId" in the Items class.. and it's bean must have a setOrderId and getOrderId... just normal, right? So, though I associated them in the mapping file, the mapping does nothing to help me, I must do it all.. right? If I must do everything, why do I have to associate? What does hibernate do for me?
Hi, you should do this in case you want inverse association. In case you have Order and Items, it is not nessesary to add property orderId for Item, hibernate will add one more columt to item table. You should define this columt in collection mapping:
Code:
<set name="items">
<key column="orderId" />
<one-to-many class="Item />
</set>
Quote:
Ok, let's say I have in my class Order a property called "orderName".. what do I do if I want to select an Item, and instead of showing it's orderId I wanna show the orderName? I know I'd normally do it with a subquery, but how do I do it using hibernate? Could you give me a query example?
In such situation you should use inverse assotiation. Its means that in Item mapping should be mapping like this:
Code:
<many-to-one property="order" class="Order" column="orderId"/>
and in Item add new property order of Order class.
this is all.