-
Nov 22nd, 2011, 01:47 PM
#1
[HIBERNATE/MySQL] Simple problem with declaring two-tabled database
Hello, I'm trying for too long to solve my simple problem, maybe someone could help me?
I have two entity objects which have to have their representation in MySQL database - Product and Meal. Product is a simple class containing a few fields. No problem.
But I have a problem with declaring Meal class as java entity class and as a MySQL table - it should contain a list of Product objects. I have no idea how to do it.
First solution is to declare field in Meal.java class, e.g. List<Product> productList, but how to implement the same solution in MySQL?
Second solution is to assume a few foreign keys in MySQL table, e.g. product1, product2, product3 <- product ID's, BUT - how to solve that on hibernate level? I'm using hibernate 3.0.
I'm really confused, I don't know how to get it...
Can anyone help me?
-
Nov 23rd, 2011, 01:16 AM
#2
And why should that be a problem?! (Next to that it seems like a homework/school assignment but that on the side).
Simple make a Meal class with a List or products and you then have to create a join table to link the meal to the products. You don't want the mail table to have product ids (where do you stop) nor do you want the product table to have meal ids (what if a product belongs to 2 meals?).
In short I suggest a read of the hibernate reference guide, especially the part explaining many-to-many relationships and join tables.
-
Nov 23rd, 2011, 04:56 AM
#3
No, it's not a homework
I'm just a real newbie and tutorials on web didn't help :]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules