@多对多表之间的关系

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

@ManyToMany Relationship Between Tables

问题

我有三个单独的实体:`dish菜品)、kitchen厨房)、restaurant餐厅)` 和连接表 `many)`。
我的问题是我如何在我的实体类中定义这种关系

**厨房模型**

@Entity
@Table(name = "kitchen")
public class Kitchen {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name = "kitchen_name")
    private String name;
    @Column(name = "kitchen_photo")
    private Blob photo;
}

**菜品模型**

@Entity
@Table(name = "dish")
public class Dish {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name = "dish_name")
    private String dishName;
    @Column(name = "dish_photo")
    private Blob dishPhoto;

**餐厅模型**

@Entity
@Table(name = "restaurant")
public class Restaurant {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name = "res_name")
    private String name;
    @Column(name = "res_rating")
    private double rating;
    @Column(name = "res_address")
    private String address;
    @Column(name = "res_number")
    private String number;
    @Column(name = "res_site")
    private String site;
    @Column(name = "res_price")
    private String price;
    @Column(name = "res_photo")
    private Blob photo;

连接表
**多对多模型**

@Entity
@Table(name = "many")
public class Many {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "res_id")
    private long res_id;
    @Column(name = "kitchen_id")
    private long kitchen_id;
    @Column(name = "dish_id")
    private long dish_id;
    @Column(name = "many_name")
    private String name;
    @Column(name = "many_rating")
    private double rating;
    @Column(name = "many_address")
    private String address;
    @Column(name = "many_number")
    private String number;
    @Column(name = "many_site")
    private String site;
    @Column(name = "many_price")
    private String price;
    @Column(name = "many_photo")
    private Blob photo;
    @Column(name = "dish_name")
    private String dishName;
    @Column(name = "kitchen_name")
    private String kitchenName;
    @Column(name = "dish_photo")
    private Blob dishPhoto;
    @Column(name = "kitchen_photo")
    private Blob kitchenPhoto;

我知道如何在两个表之间定义这种关系但对于超过两个表的情况我不太确定
我需要创建 `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

@Entity
@Table(name = "kitchen")
public class Kitchen {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "kitchen_name")
private String name;
@Column(name = "kitchen_photo")
private Blob photo;
}

Dish model

@Entity
@Table(name = "dish")
public class Dish {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "dish_name")
private String dishName;
@Column(name = "dish_photo")
private Blob dishPhoto;

Restaraunt model

 public class Restaraunt {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "res_name")
private String name;
@Column(name = "res_rating")
private double rating;
@Column(name = "res_address")
private String address;
@Column(name = "res_number")
private String number;
@Column(name = "res_site")
private String site;
@Column(name = "res_price")
private String price;
@Column(name = "res_photo")
private Blob photo;

Linking table
Many Model

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "res_id")
private long res_id;
@Column(name = "kitchen_id")
private long kitchen_id;
@Column(name = "dish_id")
private long dish_id;
@Column(name = "many_name")
private String name;
@Column(name = "many_rating")
private double rating;
@Column(name = "many_address")
private String address;
@Column(name = "namy_number")
private String number;
@Column(name = "many_site")
private String site;
@Column(name = "many_price")
private String price;
@Column(name = "many_photo")
private Blob photo;
@Column(name = "dish_name")
private String dishName;
@Column(name = "kitchen_name")
private String kitchenName;
@Column(name = "dish_photo")
private Blob dishPhoto;
@Column(name = "kitchen_photo")
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:

根据一个 `Restaurant` 可能会有多个 **dishes** 和一个 `Kitchen` 的理解

您可以像下面这样定义关系

    @Entity
    public class Restaurant {

    // 所有其他字段

    @OneToMany
    private Set<Dish> dishes;
    
    @OneToOne
    private Kitchen kitchen;
    
    }

您可以从一侧进行映射就像上面的示例一样也可以从两侧进行定义即从 `Dish``Kitchen` 两者都定义 -`Dish``Restaurant``ManyToOne`,`Kitchen``Restaurant``OneToOne`

我没有看到使用 `Many` 模型的任何理由
英文:

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

You can define the relationship like below

@Entity
public class Restaraunt {
// all other fields
@OneToMany
private Set&lt;Dish&gt; dishes;
@OneToOne
private Kitchen kitchen;
}

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:

确定