@ManyToMany 表之间的关系

Posted

技术标签:

【中文标题】@ManyToMany 表之间的关系【英文标题】:@ManyToMany Relationship Between Tables 【发布时间】:2020-08-14 12:36:36 【问题描述】:

我有三个独立的实体dish, kitchen, restaraunt 和链接表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;

餐厅模式

 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;

链接表 许多模型

@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;

我知道如何定义两个表之间的这种关系,但对于两个以上我不确定。 我需要创建表Many 吗?

【问题讨论】:

请参考this,它将帮助您了解关联映射。 【参考方案1】:

要理解Restaurant 将有许多菜肴 和一个Kitchen

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

@Entity
public class Restaraunt 

// all other fields

@OneToMany
private Set<Dish> dishes;

@OneToOne
private Kitchen kitchen;


您可以像上面一样从一侧进行映射,也可以从两侧进行映射,即从DishKitchen 以及DishRestaurant 作为ManyToOneKitchen 到@987654330 @如OneToOne

我看不出有任何理由使用Many 模型。

【讨论】:

抱歉,我指的不是厨房,而是食物,比如亚洲食物、欧洲食物等。但我明白了你的想法,谢谢! 你的问题在哪里提到?您所说的食物类型应属于Dish。你可以在Dish 模型下有一个字段private String dishType,它可以达到目的。您可以在 dishType 字段中将值设置为亚洲、欧洲。

以上是关于@ManyToMany 表之间的关系的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate:ManyToMany 反向删除

Hibernate学习笔记 --- 创建基于中间关联表的多对多映射关系

laravel hasmanythrough on manytomany

Symfony 的 make:entity 命令创建的 ManyToMany 关系之间的握手

Django 管理界面:使用带中间表的 ManyToMany 字段的 Horizo​​ntal_filter

用于 ManyToMany 的 Symfony Doctrine 重命名表