【问题标题】:One-To-Many relationship with additional table and extra column. How to represent it in Java class?与附加表和附加列的一对多关系。如何在 Java 类中表示它?
【发布时间】:2021-04-02 18:41:51
【问题描述】:

我正在开发卡路里计数器应用程序。登录后,每个用户只能访问他们的饭菜。

我有一个如下数据库:

我会像下面的模板一样显示所有信息。

Meal 1
Chicken breast 100g 130kcal
Apple 200g 100kcal
Tomatoes 300g 30kcal
Meal 2
Chicken breast 100g 130kcal
Apple 200g 100kcal
Tomatoes 300g 30kcal

用户类别:

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

MealFoodProcuct 类

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

我的问题:

  1. 连接这些类的方法是否正确?
  2. MealFoodProduct 类中的 ID 应该是什么?

【问题讨论】:

  • 您应该使用composite identifier 作为MealFoodProduct
  • 有三个可能的选项:1) EmbeddedId / @Embeddable 2) @IdClass 或 3) Hibernate 允许通过多个 @Id 定义没有“主键类”的复合标识符属性。

标签: java mysql spring spring-boot hibernate


【解决方案1】:

我建议您对 MealFoodProduct 实体使用带有 @IdClass 的复合标识符。

@Entity
@Table(name = "meals_food_products")
@IdClass( MealFoodProductPK.class )
public class MealFoodProduct {
    
    @Id
    @ManyToOne
    @JoinColumn(name = "meal_id")
    public Meal getMeal() {
        return meal;
    }

    @Id
    @ManyToOne
    @JoinColumn(name = "food_product_id")
    public FoodProduct getFoodProduct() {
        return foodProduct;
    }

    // ...
}

public class MealFoodProductPK implements Serializable {

   private Meal meal;
   private FoodProduct foodProduct;

   public MealFoodProductPK(Meal meal, FoodProduct foodProduct) {
      this.meal = meal;
      this.foodProduct = foodProduct;
   }

   public MealFoodProductPK() {
   }

   //Getters, setters, equals, hashCode are omitted for brevity
}

更多详情见here

附:映射的其他部分看起来不错。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2011-09-01
    • 2023-03-16
    • 2011-10-13
    • 1970-01-01
    相关资源
    最近更新 更多