-->
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.  [ 2 posts ] 
Author Message
 Post subject: Collection design question
PostPosted: Tue Mar 22, 2005 6:06 pm 
Newbie

Joined: Tue Mar 22, 2005 6:01 pm
Posts: 13
Location: Sydney, Australia
Hi everyone,

I'm a newbie and i just have a design question to do with collections. I just want to make sure my design is right before i start my hibernate implementation so any advice you can provide will be greatly appreciated.

I have two tables, Order and OrderLine with a one-to-many relationship.

Currently the tables look something like this.

Order
---------
order_no
order_id (<----primary key)
cust_no
ord_date
.
(and so on)
.

OrderLine
---------
order_id (foreign key)
line_no
product
qty


The combination of order_id/line_no on the OrderLine table form a uniqueness constraint (since you can't have two lines with the same line_no for the same order_id).

What i'm trying to figure out is what is the "best" way to implement this relationship. Currently I store all the Orderlines in an ArrayList in my java Order class. But apparently i can't use a List to implement this in hibernate since i don't have a (0,1,2,3,...) index. From what i understand so far, i should add a single field primary key to the OrderLine table and implement it as a set.

Any suggestions??

Cheers
Johnny


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 22, 2005 7:21 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
You don't actually have to add an ID to your line item table if you don't want to. You can also model it as a collection of components.

It may make sense to make the Line Item an entity (with its own ID) if you use it independently in your application, or if several other entities reference it. If you decide to go this route then a simple Set mapped with inverse=true can work. If you need to stay with theh List interface for compatibility, you could try mapping it as a Bag, but I'd try to stick with Set because it's the most straightforward.

If, on the other hand, you decide to map it as a collection of components, Line Item will not need its own synthetic key. The Hibernate reference guide has a very good entry on mapping dependent objects that are not modelled as entities:

http://www.hibernate.org/hib_docs/reference/en/html_single/#components-incollections

I've used both approaches successfully. You'll just have to ask yourself if Line Item deserves to be an entity or not.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.