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: Bad database design with many-to-many associations?
PostPosted: Fri May 14, 2004 11:54 am 
Newbie

Joined: Tue May 04, 2004 6:31 pm
Posts: 11
Hello All,

Here is my question...

In creating the association table for a many-to-many relationship, why doesn't hibernate use the primary keys of the two tables for the composite key of the association table?

Note: I have only seen this problem when I need an Association Class for attributes such as Quantity, ItemPrice, or Date... I have not seen this problem when I don't need an Association class. Note that I am using Lists, not Sets (Hibernate did okay when I used a set and no Association class, but when I used a List with an Association class, I get this problem).

---

Here are the details...

I have spent the past three days trying to implement my simple many to many association using hibernate. I have programmed this before by writing my own DAOs, but I thought using hibernate would be a great idea.

Well, it turns out that hibernate was (and still is) generating the primary keys of the association table in a different manner than what I learned.

I have an Order class, a Product class, and a LineItem class (LineItem is the association class between Order and Product). Normally, one would take the primary key from Order and the primary key from Product and use that as the composite key for LineItem.

Why doesn't hibernate use the two keys from the Order table and Product table when using java.util.Lists? (My Order class has a List of LineItem objects). Hibernate does not use the keys from the two tables (Order and Product) to form the composite key for LineItem.

You can see this problem(?) for yourself in the Hibernate example in the Hibernate Documenation... http://www.hibernate.org/hib_docs/refer ... derproduct . Just look at the Database Schema below the hibernate mapping example, and you can see that for the table line_items, product_id and order_id is NOT the primary key.

Code:
create table line_items (
    line_number INTEGER not null,
    order_id BIGINT not null,
    product_id BIGINT,
    quantity INTEGER,
    primary key (order_id, line_number)
)



Is this normal? Is this appropriate? Is this just a new way that I haven't seen before? (I know it works... but it is different than what I am used to).

Thanks!
--
Nathan


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 12:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This is perfectly correct, a List can contain the same element multiple times at different indexes. Use a Set otherwise.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 1:00 pm 
Newbie

Joined: Tue May 04, 2004 6:31 pm
Posts: 11
Okay... it makes sense. Thanks!


--
Nathan


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.