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