英文:
@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<Dish> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论