I have this Entity
@Entity
public class MealPlanADay {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private Long id;
//@OneToMany(cascade = CascadeType.ALL, orphanRemoval = false)
@ElementCollection(targetClass = Recipe.class)
@MapKeyEnumerated()
private Map<Meal,List<Recipe>> recipes=new EnumMap<>(Meal.class);
//Other fields
public Map<Meal, List<Recipe>> getRecpies() {
return recipes;
}
public void setRecipes(Map<Meal, List<Recipe>> recipes) {
this.recipes = recipes;
}
public void AddRecipe(Meal meal,Recipe recipe){
if(this.recipes.containsKey(meal)){
List<Recipe> recipesList = this.recipes.get(meal);
recipesList.add(recipe);
this.recipes.put(meal, recipesList);
}else{
this.recipes.putIfAbsent(meal, List.of(recipe));
}
}
public void removeRecipe(Meal meal,Recipe recipe){
if(this.recipes.containsKey(meal)){
List<Recipe> recipesList = this.recipes.get(meal);
recipesList.remove(recipe);
this.recipes.put(meal, recipesList);
}else{
}
}
Meal is an enum ex “Breakfast,Lunch,Dinner”. Recipe is another normal entity.
My goal is for each Meal lunch or dinner, I can have multiple recipes associated with that Meal enum.
The result DDL of this code above is this picture below
enter image description here
enter image description here
It looks wrong since I have a list of recipes not a single recipe. also persist this MealPlanADay entity result in
org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Long org.omar.recipes.recipe.entity.Recipe.id] by reflection for persistent property [org.omar.recipes.recipe.entity.Recipe#id] : [Recipe{id=8, name=Chicken Caesar Salad, category=Lunch,Dinner,Breakfast}]
as for this error I don’t know why its happening since I the id of recipe IS accessible. and normal Repository of this entity is working fine.
Should I **rethink ** my MealPlanADay entity or is there solution for this?
Thank you.