Here it is:
PersistenceManager pm = PMF.get().getPersistenceManager();
List<UserStats> result = (List<UserStats>) pm.newQuery( "select from "
+ UserStats.class.getName() + " where id == '" + session.getId() +
"'" ).execute();
if( result.isEmpty() )
stats = new UserStats( session.getId(), System.currentTimeMillis
() );
else
stats = result.get( 0 );
Integer counter = stats.getQueries().get( query );
if( counter == null )
counter = new Integer( 1 );
else
counter = new Integer( counter.intValue() + 1 );
stats.getQueries().put( query, counter );
try
{
pm.makePersistent( stats );
}
finally
{
pm.close();
}
On Oct 30, 8:47 pm, "Jason (Google)" <[email protected]> wrote:
> Can you post the code that you're using to re-persist the updated HashMap?
>
> - Jason
>
>
>
> On Thu, Oct 29, 2009 at 6:07 AM, barak <[email protected]> wrote:
>
> > Thanks, did that and the map is indeed serialized now. But now, the
> > enitity is fetched, seems like the state is not always kept.
>
> > For example, I would like to store some attribute from an HttpSession
> > using the UserStats instance. Every time a user in a session press
> > some button, a instance is fetched (using the session id as an
> > identifier) and update a counter in the HashMap. The problem I faced
> > is even that the object is found by the JDO, the counter updated and
> > the object persisted again, next fetch does not return the instance
> > with the updated counter. Can you help please debugging this?
>
> > This is the data object:
>
> > @PersistenceCapable(identityType = IdentityType.APPLICATION)
> > public class UserStats
> > {
> > �...@primarykey
> > �...@persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > private Key key;
>
> > �...@persistent
> > private String id;
>
> > �...@persistent
> > private Long time;
>
> > �...@persistent( serialized ="true" )
> > private HashMap<String, Integer> queries;
>
> > public UserStats( String id, Long time )
> > {
> > this.id = id;
>
> > this.time = time;
>
> > queries = new HashMap<String, Integer>();
> > }
>
> > public Key getKey()
> > {
> > return key;
> > }
>
> > public String getId()
> > {
> > return id;
> > }
>
> > public void setId( String id )
> > {
> > this.id = id;
> > }
>
> > public Long getTime()
> > {
> > return time;
> > }
>
> > public void setTime( Long time )
> > {
> > this.time = time;
> > }
>
> > public HashMap<String, Integer> getQueries()
> > {
> > return queries;
> > }
>
> > public void setQueries( HashMap<String, Integer> queries )
> > {
> > this.queries = queries;
> > }
> > }
>
> > On Oct 29, 10:38 am, Patrizio Munzi <[email protected]> wrote:
> > > HashMap isn't supported as a persistable type.
> > > The only way you've got to persist it is serialize it:
> >http://gae-java-persistence.blogspot.com/2009/10/serialized-fields.html
> > > 1KViewDownload- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---