http://groups.google.com/group/google-appengine-java/browse_thread/thread/fd8043e63ff6c22e/

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 <seler...@gmail.com> 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/thread/fd8043e63ff6c22e/
>
> 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
> google-appengine-j...@googlegroups.com.
> > To unsubscribe from this group, send email to
> google-appengine-java+unsubscr...@googlegroups.com<google-appengine-java%2bunsubscr...@googlegroups.com>
> .
> > For more options, visit this group athttp://
> groups.google.com/group/google-appengine-java?hl=en.
>
> --
> 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
> google-appengine-j...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengine-java+unsubscr...@googlegroups.com<google-appengine-java%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en.
>
>

-- 
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 google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to