package pl.mycalories.model;

import com.fasterxml.jackson.annotation.JsonManagedReference;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;

@Entity
@SequenceGenerator(name = "idgen", sequenceName = "meal_id_seq", allocationSize = 1)
public class Meal extends AbstractModel {

    @NotNull
    private String name;

    @Embedded
    private NutritionalValues nutritionalValues;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "meal", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonManagedReference
    private Set<ProductMeal> productMeals = new HashSet<ProductMeal>();

//    @ManyToOne
//    @JoinColumn(name = "daily_calories_id")
//    private DailyCalories dailyCalories;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public NutritionalValues getNutritionalValues() {
        return nutritionalValues;
    }

    public void setNutritionalValues(NutritionalValues nutritionalValues) {
        this.nutritionalValues = nutritionalValues;
    }

    public Set<ProductMeal> getProductMeals() {
        return productMeals;
    }

    public void setProductMeals(Set<ProductMeal> productMeals) {
        this.productMeals = productMeals;
    }

//    public DailyCalories getDailyCalories() {
//        return dailyCalories;
//    }
//
//    public void setDailyCalories(DailyCalories dailyCalories) {
//        this.dailyCalories = dailyCalories;
//    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Meal)) return false;
        if (!super.equals(o)) return false;

        Meal meal = (Meal) o;

        return name != null ? name.equals(meal.name) : meal.name == null;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}

@@ -21,24 +21,84 @@
 s.model;%0D%0A%0D%0A
+import com.fasterxml.jackson.annotation.JsonBackReference;%0D%0A
 import com.f
@@ -733,26 +733,24 @@
 Meal%3E();%0D%0A%0D%0A
-//
     @ManyToO
@@ -751,20 +751,78 @@
 anyToOne
+(fetch = FetchType.LAZY, targetEntity = DailyCalories.class)
 %0D%0A
-//
     @Joi
@@ -858,18 +858,40 @@
 s_id%22)%0D%0A
-//
+    @JsonBackReference%0D%0A
     priv
@@ -1508,18 +1508,16 @@
    %7D%0D%0A%0D%0A
-//
     publ
@@ -1551,26 +1551,24 @@
 lories() %7B%0D%0A
-//
         retu
@@ -1590,23 +1590,17 @@
 s;%0D%0A
-//
     %7D%0D%0A
-//%0D%0A//
+%0D%0A
     
@@ -1660,18 +1660,16 @@
 ies) %7B%0D%0A
-//
         
@@ -1709,10 +1709,8 @@
 s;%0D%0A
-//
     
@@ -1948,64 +1948,203 @@
     
-return name != null ? name.equals(meal.name) : meal.name
+if (name != null ? !name.equals(meal.name) : meal.name != null) return false;%0D%0A        return nutritionalValues != null ? nutritionalValues.equals(meal.nutritionalValues) : meal.nutritionalValues
  == 
@@ -2309,24 +2309,120 @@
 de() : 0);%0D%0A
+        result = 31 * result + (nutritionalValues != null ? nutritionalValues.hashCode() : 0);%0D%0A
         retu

add relationship between Meal and DailyCalories classes + change equals and hashCode