-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Saving a field which is a Map
PostPosted: Fri Mar 14, 2008 6:24 am 
Newbie

Joined: Fri Mar 14, 2008 5:48 am
Posts: 2
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:


Top
 Profile  
 
 Post subject: Fixed
PostPosted: Fri Mar 14, 2008 7:00 am 
Newbie

Joined: Fri Mar 14, 2008 5:48 am
Posts: 2
The problem was not related to the mapping, but to the implementation of the Tdao.save(T t). I use Springs HibernateDaoSupport, and I had to use getHibernateTemplate.saveOrUpdate(t) instead of beginning a transaction etc.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.