我正在研究卡路里计数器的应用程序。登录后,每个用户只能访问他们的膳食。
我的数据库如下:
我将像下面的模板一样显示所有信息。
餐食1鸡胸100g130kCalapple200g100kCaltoes300g30kCal
餐食2鸡胸100g130kCalapple200g100kCaltomoes300g30kCal
用户类别:
@Entity
@Table(name = "users")
public class User {
private int id;
private String username;
private String password;
private int age;
private String email;
private Gender gender;
private List<Meal> meals;
public User() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "username")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "age")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Column(name = "email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name = "gender")
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@OneToMany(mappedBy = "user")
public List<Meal> getMeals() {
return meals;
}
public void setMeals(List<Meal> meals) {
this.meals = meals;
}
private enum Gender{
MALE,
FEMALE
}
}
用餐类别:
@Entity
@Table(name = "meals")
public class Meal {
private int id;
private LocalDate date = LocalDate.now();
private User user;
private List<MealFoodProduct> mealFoodProducts;
public Meal() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@OneToMany(mappedBy = "foodProduct")
public List<MealFoodProduct> getMealFoodProducts() {
return mealFoodProducts;
}
public void setMealFoodProducts(List<MealFoodProduct> mealFoodProducts) {
this.mealFoodProducts = mealFoodProducts;
}
@Column(name = "created_at")
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
@ManyToOne
@JoinColumn(name = "user_id")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
食品类:
@Entity
@Table(name = "food_products")
public class FoodProduct {
private int id;
private String productName;
private Double proteins;
private Double fats;
private Double carbohydrates;
private Double calories;
public FoodProduct() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "product_name")
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
@Column(name = "proteins")
public Double getProteins() {
return proteins;
}
public void setProteins(Double proteins) {
this.proteins = proteins;
}
@Column(name = "fats")
public Double getFats() {
return fats;
}
public void setFats(Double fats) {
this.fats = fats;
}
@Column(name = "carbohydrates")
public Double getCarbohydrates() {
return carbohydrates;
}
public void setCarbohydrates(Double carbohydrates) {
this.carbohydrates = carbohydrates;
}
@Override
public String toString() {
return productName;
}
@Column(name = "calories")
public Double getCalories() {
return calories;
}
public void setCalories(Double calories) {
this.calories = calories;
}
}
MealFoodProct类
@Entity
@Table(name = "meals_food_products")
public class MealFoodProduct {
private Meal meal;
private FoodProduct foodProduct;
private double weight;
public MealFoodProduct() {
}
@ManyToOne
@JoinColumn(name = "meal_id")
public Meal getMeal() {
return meal;
}
public void setMeal(Meal meal) {
this.meal = meal;
}
@ManyToOne
@JoinColumn(name = "food_product_id")
public FoodProduct getFoodProduct() {
return foodProduct;
}
public void setFoodProduct(FoodProduct foodProduct) {
this.foodProduct = foodProduct;
}
@Column(name = "food_product_weight")
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
我的问题:
连接这些类的方法正确吗?
mealfoodproduct类中的id应该是什么?
1条答案
按热度按时间lsmepo6l1#
我建议您使用复合标识符
@IdClass
对于MealFoodProduct
实体。其他详细信息请参见此处。
p、 你的Map的其他部分看起来不错。