views:

45

answers:

1

The problem: let's say I have two tables Client, and Product, in which Client has its primary key and a column called products (that points to pk's in Product table)... ok, if I need products to point only one row, it's nice, but if I need it to point for... 1000 rows in Product table, the products column would have to be larger enough... but I can't predict this situation.

So, how could I design my table and how would I use hibernate with it, to achieve that "pointing" in a optmized and maybe "easy" way.

NOTE: I excluded some columns of the "design" presented here, just to keep the simplicity.

NOTE 2: Each Client's row is unique, and only one Client is related to a Product's row, that is, Product will be of one client and of nobody else.

+1  A: 

This is a many-to-one relationship. You can reference the Client from the Product table instead of the other way around.

Then, you can setup a one-to-many relationship in your hibernate mapping file. Here is a nice example about how to do that: http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-many-1.html

Kevin Crowell
Thanks for the tip; I won't use many-to-many (as it doesn't look to fit my problem), but instead, one-to-many, that can be found here: http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-many-1.html
Wilhelm
Sorry, I assumed that a single Product would be able to be associated with multiple Clients. If not, then you are right, you would need to use a one-to-many relationship. In this case, you could have the Product table point to the Client table. You would not need a mapping table.
Kevin Crowell
@Wilhelm Why did you accept this answer in its current state then (no offense to the poster)?
Pascal Thivent
@Pascal I agree. He may have been able to discover his answer based on what I posted, but my answer is technically not the right solution.
Kevin Crowell
Ok, I just did it as a act of thanks. Anyway, thank you for the tips.
Wilhelm
I updated my answer to hopefully reflect the solution better.
Kevin Crowell
@Wilhelm Just ask @Kevin to fix the answer (which was my point) before to accept it (for the sake of correctness of archives). Posters will usually do it if kindly asked :)
Pascal Thivent