Hi everybody,
I have a POJO called Meal, and another POJO called Food. The meal has among other fields a map called mealComponenets, in which a specific food (e.g. potato) is the key, and the amount of this food is the value (e.g. 10, for 10%). My getter method looks like this.
Code:
@CollectionOfElements
@MapKeyManyToMany(targetEntity = Food.class)
@Column(name="amount", nullable=false)
public Map<Food, Integer> getMealComponents() {
return this.mealComponents;
}
When I try this with <property name="hbm2ddl.auto">create</property>, the tables get created correctly (i.e. a Meal table, a Food table, and a Meal_mealComponents table). When I put some values inte the mealComponents table manually the Meal also loads correctly with its MealComponents. However, I have a problem when trying to save a meal.
Code:
Food potato = new Food();
potato.setName("potato");
foodDao.save(potato);
Food meat = new Food();
meat.setName("meat");
foodDao.save(meat);
Map<Food, Integer> mealComponents = new HashMap<Food, Integer>();
mealComponents.put(potato, 80);
mealComponents.put(meat, 20);
Meal meal = new Meal();
meal.setMealComponents(mealComponents);
meal.setName("meal1");
mealDao.save(meal);
Now the potato and the meat gets saved correctly, and the meal gets a row in the Meal table, but the Meal_MealComponents table is empty, and no error occurs. According to hibernate logging, it does not seem as if hibernate is trying to save anything to the Meal_mealComponents table. What am I doing wrong?
I have looked around in the documentation on the Internet and on this forum trying to solve this problem, but without success.
Code: