Results 1 to 3 of 3

Thread: [HIBERNATE/MySQL] Simple problem with declaring two-tabled database

  1. #1
    Join Date
    Nov 2011
    Posts
    8

    Default [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?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2011
    Posts
    8

    Default

    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
  •