The following posting by Max Ross of the Google App Engine engineering team is extremely useful for the case of all serialized fields (which is the only way you can store HashMap-s in GAE (at least so far)):
JDO/JPA Snippets That Work - Serialized Fields http://bit.ly/9fMbPs On Apr 18, 5:11 pm, mscwd01 <[email protected]> wrote: > Thanks for your informative reply. > > So basically, the only solution you found was to create a copy of the > whole object holding the HashMap, persist the new object and delete > the old object? > I sincerely hope I have misunderstood you as this is a terrible way to > have to go about it. > > Is there any other way, you can update a HashMap rather than creating > a whole new object to persist? > > Thanks again for your help with this. > > On Apr 18, 6:50 am, "sreenidhi b.s" <[email protected]> wrote: > > > > >http://groups.google.com/group/google-appengine-java/browse_thread/th... > > > the link above works only for persisting and retrieving hash maps ,but i did > > not find any "proper" solution for updating and persisting the updated > > hashmaps. > > > "You have to provide a new reference to the updated hashmap ,since datastore > > cannot identify between updated and stale hashmap." > > But it did not work in my case and i urge you to try the same before you try > > alternatives. > > > The Alternative i found was , > > 1) retrieve the persisted hashmap , > > 2) create a new hashmap reference to updated hashmap > > 3) make the new reference persistent. > > 4)delete the previous reference. > > > code: /* i have used some custom classes ,but the you should be able to > > understand the logic i've employed to get the solution */ > > > /*Retrieve the persisted HashMap*/ > > PersistenceManager pm0= PMF.get().getPersistenceManager(); > > Query query0 = pm0.newQuery(VocabHashMap.class); > > List<VocabHashMap> results1 = (List<VocabHashMap>) query0.execute(); > > /*if there are no hashmaps, create a new one */ > > if (results1.isEmpty()) > > { > > > HashMap<String, Double> map = new HashMap<String, Double>(); > > for (String a : linelist) { > > Double freq = map.get(a); > > map.put(a, (freq == null) ? 1 : freq + 1); > > } > > VocabHashMap vhm =new VocabHashMap(map); > > pm0.makePersistent(vhm); > > pm0.close(); > > > } > > > else{ > > > for (VocabHashMap vhm : results1) { > > > /*if you already have a hashmap,then update it */ > > > HashMap<String,Double> map1=vhm.getMap(); /* assign a new reference to > > retrieved hashmap */ > > > for (String a : linelist) { > > Double freq = map1.get(a); > > map1.put(a, (freq == null) ? 1 : freq + 1); > > > } > > > VocabHashMap vhm1=new VocabHashMap(map1); /* create a new hashmap*/ > > pm0.makePersistent(vhm1); /*save the updated one */ > > pm0.deletePersistent(vhm); /*delete the old reference */ > > > } > > > pm0.close(); > > > } > > On Sun, Apr 18, 2010 at 10:41 AM, seleronm <[email protected]> wrote: > > > Hi, > > > > I was useful referring to this thread. > > > You might be also useful for it. > > > >http://groups.google.com/group/google-appengine-java/browse_thread/th... > > > > Hope some of this helps. > > > > thanks. > > > > > I am not able to persist a HashMap field. This is how I define it: > > > > > @Persistent(serialized = "true", defaultFetchGroup = "true") > > > > private Map<String, Integer> items; > > > > > When I create the Object which the Map is a field of, I instantiate > > > > the Map as follows: > > > > > items = new HashMap<String, Integer>(); > > > > > When I update the "items" HashMap I try to persist the object by doing > > > > the following: > > > > > PersistenceManager pm = PMF.get().getPersistenceManager(); > > > > // Query for Object (HashMap is a field of this Object) > > > > // Update HashMap > > > > pm.makePersistent(objectContainingHashMap); > > > > > However, when I attempt to retrieve the object after persisting it and > > > > read the HashMap it is always empty, as though I never updated it. > > > > > Has anyone experienced this before? > > > > > Thanks -- You received this message because you are subscribed to the Google Groups "Google App Engine for Java" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
