These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: [Hibernate][3.1] Simple bidirectionnal parent-child
PostPosted: Thu Mar 02, 2006 11:32 am 
Newbie

Joined: Sat Feb 25, 2006 6:46 am
Posts: 9
Hello.

Hibernate's examples are many... but nevertheless, i did find anything I want...

I'm trying top build a very simple relationship with two objects :
- orders, with an assigned string id
- order items, whose primary key is the order id and a number

To make it as simple as possible, i use a simple composite association like this for the order mapping :

<list
name="itemList"
table="ORDER_ITEMS"
lazy="false"
cascade="all">
<key column="ORDER_ID" />
<list-index column="ITEM_NUMBER" />
<composite-element class="OrderItem">
<property
name="description"
length="32" />
</composite-element>
</list>

And it works as expected : items are inserted, with a foreign order-id key and generated index; they are updated and deleted too when the order list changes.

Now, i'd like to add a return path from the order item to its order : client classes that handle items need to get back to the containing order... If i'm not mistaken, this could be done in other cases using a "many-to-one" on an entity.

How can i add this to my order item ? Should i change it all to use two entities ? I've also tried it, but the item composite key is tedious, and doesn't seem to support any form of automatic generation...

Thanks for your help.

--
Ephemeris Lappis

_________________
Ephemeris Lappis


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 12:06 pm 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
You can do it as follows:
Code:
   
<list name="itemList" table="ORDER_ITEMS" lazy="false" cascade="all">
  <key column="ORDER_ID" />
  <list-index column="ITEM_NUMBER" />
  <composite-element class="OrderItem">
    <parent name="order">
    <propertyname="description" length="32" />
  </composite-element>
</list>


As HiA states, true bidirectional navigation is not possible in this case. You cannnot retrieve an OrderItem independently. For that you would need to use entities.

Hope this helps!
Urs


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 12:47 pm 
Newbie

Joined: Sat Feb 25, 2006 6:46 am
Posts: 9
First, thanks for your answer !

I've tested the parent attribute, and it works just as i wanted.
Now, if i need to link more data on my order item, say the 'many' part of the relationship, a pure entity is really a better way. So i've tried alternative way to declare two entities. In this case, item the composite key must be handled by the application : declare it, of course, but also generate values too. I've not found any way to make the item number handled according to the list elements.

Another idea ?

Thanks again.

_________________
Ephemeris Lappis


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.