@多对多表之间的关系

huangapple go评论96阅读模式
英文:

@ManyToMany Relationship Between Tables

问题

  1. 我有三个单独的实体`dish菜品)、kitchen厨房)、restaurant餐厅)` 和连接表 `many)`
  2. 我的问题是我如何在我的实体类中定义这种关系
  3. **厨房模型**
  4. @Entity
  5. @Table(name = "kitchen")
  6. public class Kitchen {
  7. @Id
  8. @GeneratedValue(strategy = GenerationType.IDENTITY)
  9. private long id;
  10. @Column(name = "kitchen_name")
  11. private String name;
  12. @Column(name = "kitchen_photo")
  13. private Blob photo;
  14. }
  15. **菜品模型**
  16. @Entity
  17. @Table(name = "dish")
  18. public class Dish {
  19. @Id
  20. @GeneratedValue(strategy = GenerationType.IDENTITY)
  21. private long id;
  22. @Column(name = "dish_name")
  23. private String dishName;
  24. @Column(name = "dish_photo")
  25. private Blob dishPhoto;
  26. **餐厅模型**
  27. @Entity
  28. @Table(name = "restaurant")
  29. public class Restaurant {
  30. @Id
  31. @GeneratedValue(strategy = GenerationType.IDENTITY)
  32. private long id;
  33. @Column(name = "res_name")
  34. private String name;
  35. @Column(name = "res_rating")
  36. private double rating;
  37. @Column(name = "res_address")
  38. private String address;
  39. @Column(name = "res_number")
  40. private String number;
  41. @Column(name = "res_site")
  42. private String site;
  43. @Column(name = "res_price")
  44. private String price;
  45. @Column(name = "res_photo")
  46. private Blob photo;
  47. 连接表
  48. **多对多模型**
  49. @Entity
  50. @Table(name = "many")
  51. public class Many {
  52. @Id
  53. @GeneratedValue(strategy = GenerationType.IDENTITY)
  54. private long id;
  55. @Column(name = "res_id")
  56. private long res_id;
  57. @Column(name = "kitchen_id")
  58. private long kitchen_id;
  59. @Column(name = "dish_id")
  60. private long dish_id;
  61. @Column(name = "many_name")
  62. private String name;
  63. @Column(name = "many_rating")
  64. private double rating;
  65. @Column(name = "many_address")
  66. private String address;
  67. @Column(name = "many_number")
  68. private String number;
  69. @Column(name = "many_site")
  70. private String site;
  71. @Column(name = "many_price")
  72. private String price;
  73. @Column(name = "many_photo")
  74. private Blob photo;
  75. @Column(name = "dish_name")
  76. private String dishName;
  77. @Column(name = "kitchen_name")
  78. private String kitchenName;
  79. @Column(name = "dish_photo")
  80. private Blob dishPhoto;
  81. @Column(name = "kitchen_photo")
  82. private Blob kitchenPhoto;
  83. 我知道如何在两个表之间定义这种关系但对于超过两个表的情况我不太确定
  84. 我需要创建 `Many` 表吗
英文:

I have three separate entities dish, kitchen, restaraunt and linking table many.
My question is, how can I define this relation in my entity classes?

Kitchen model

  1. @Entity
  2. @Table(name = "kitchen")
  3. public class Kitchen {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private long id;
  7. @Column(name = "kitchen_name")
  8. private String name;
  9. @Column(name = "kitchen_photo")
  10. private Blob photo;
  11. }

Dish model

  1. @Entity
  2. @Table(name = "dish")
  3. public class Dish {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private long id;
  7. @Column(name = "dish_name")
  8. private String dishName;
  9. @Column(name = "dish_photo")
  10. private Blob dishPhoto;

Restaraunt model

  1. public class Restaraunt {
  2. @Id
  3. @GeneratedValue(strategy = GenerationType.IDENTITY)
  4. private long id;
  5. @Column(name = "res_name")
  6. private String name;
  7. @Column(name = "res_rating")
  8. private double rating;
  9. @Column(name = "res_address")
  10. private String address;
  11. @Column(name = "res_number")
  12. private String number;
  13. @Column(name = "res_site")
  14. private String site;
  15. @Column(name = "res_price")
  16. private String price;
  17. @Column(name = "res_photo")
  18. private Blob photo;

Linking table
Many Model

  1. @Id
  2. @GeneratedValue(strategy = GenerationType.IDENTITY)
  3. private long id;
  4. @Column(name = "res_id")
  5. private long res_id;
  6. @Column(name = "kitchen_id")
  7. private long kitchen_id;
  8. @Column(name = "dish_id")
  9. private long dish_id;
  10. @Column(name = "many_name")
  11. private String name;
  12. @Column(name = "many_rating")
  13. private double rating;
  14. @Column(name = "many_address")
  15. private String address;
  16. @Column(name = "namy_number")
  17. private String number;
  18. @Column(name = "many_site")
  19. private String site;
  20. @Column(name = "many_price")
  21. private String price;
  22. @Column(name = "many_photo")
  23. private Blob photo;
  24. @Column(name = "dish_name")
  25. private String dishName;
  26. @Column(name = "kitchen_name")
  27. private String kitchenName;
  28. @Column(name = "dish_photo")
  29. private Blob dishPhoto;
  30. @Column(name = "kitchen_photo")
  31. private Blob kitchePhoto;

I know how to define this relationship between two tables, but for more than two I'm not sure.
Am I need to create table Many ?

答案1

得分: 3

Here's the translated code portion:

  1. 根据一个 `Restaurant` 可能会有多个 **dishes** 和一个 `Kitchen` 的理解
  2. 您可以像下面这样定义关系
  3. @Entity
  4. public class Restaurant {
  5. // 所有其他字段
  6. @OneToMany
  7. private Set<Dish> dishes;
  8. @OneToOne
  9. private Kitchen kitchen;
  10. }
  11. 您可以从一侧进行映射就像上面的示例一样也可以从两侧进行定义即从 `Dish` `Kitchen` 两者都定义 - `Dish` `Restaurant` `ManyToOne` `Kitchen` `Restaurant` `OneToOne`
  12. 我没有看到使用 `Many` 模型的任何理由
英文:

Going by the understanding that a Restaurant will have many dishes and one Kitchen

You can define the relationship like below

  1. @Entity
  2. public class Restaraunt {
  3. // all other fields
  4. @OneToMany
  5. private Set&lt;Dish&gt; dishes;
  6. @OneToOne
  7. private Kitchen kitchen;
  8. }

You can have the mapping from one side like above or define it from both sides i.e. from Dish and Kitchen as well - with Dish to Restaurant as ManyToOne and Kitchen to Restaurant as OneToOne

I don't see any reason for having the Many model.

huangapple
  • 本文由 发表于 2020年8月14日 20:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63412893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定