I was asked, via email, to post the class that used this and the code
to use the function above:
The field this was directed to had a member like the following:
@Persistent(serialized = "true",defaultFetchGroup="true")
private Map<String,String> callbackUrlMap = new HashMap<String,String>
();
I used the above function in another function that I call a
GenericDao. It was my understanding that inserting works fine, it's
when you try to update that it has problems. Note that there is some
copying below because I'm using GWT and the enhanced classes don't go
over the GWT wire without removing the extra stuff added by JDO. It is
as follows (bear in mind, this is a WIP):
/**
* Gets the database version based off the object passed in, copies
* the argument object to the database object, then persists any
changes.
*
* @param clazz - class being persisted
* @param object - object to be persisted
* @return
* @throws DaoException
*/
public T update(Class<T> clazz, T object) throws DaoException {
assert ClientUtil.isNotEmpty(object.getId());
Log.debug("update=["+object+"]");
PersistenceManager pm = PMF.get().getPersistenceManager();
T returnT = null;
try {
T dbObject = null;
try {
dbObject = pm.getObjectById(clazz,
object.getId());
} catch(JDOObjectNotFoundException e){
throw new DaoException("Object does not exist
in the database,
can't update it",e);
}
// Dozer's copy adds to arrays, it doesn't remove
(unless a schema
file is specified)
//copy(object, dbObject); // copy fields from existing
object to
database object (making any changed fields "dirty")
object.setId(dbObject.getId()); // just copy the
ID...this may end
up bad
JDOHelper.makeSerializedDirty(object);
returnT = copy(object.getClass(),
pm.makePersistent(object));
} catch(Exception e){
Log.error("Unable to update object ["+object+"]",e);
throw new DaoException("Unable to update object",e);
} finally {
PMF.tearDown(pm);
}
Log.debug("updated object with ID=["+object.getId()+"]");
return returnT;
}
-Nick
On Nov 24, 8:51 am, Nick <[email protected]> wrote:
> I had the same problem and came up with this snippet of code to work
> around the issue. Use it when you are updating your object:
>
> /**
> * Iterates through the object and makes any persistent annotated
> objects that are serialized = "true"
> * dirty using <code>javax.jdo.JDOHelper.makeDirty</code>.
> * @param <T>
> * @param tInstance
> */
> public static <T> void makeSerializedDirty(T tInstance){
> for (Field field : tInstance.getClass().getDeclaredFields()) {
> Persistent persistent =
> field.getAnnotation(Persistent.class);
> if(persistent != null &&
> Boolean.valueOf(persistent.serialized())){
> javax.jdo.JDOHelper.makeDirty(tInstance,
> field.getName());
> }
> }
> }
>
> -Nick
>
> On Oct 11, 7:34 pm, doc <[email protected]> wrote:
>
>
>
> > This sample work but could not update content of HashMap
>
> > Thank you Jason to provide useful sample. Based on this sample I have
> > implemented my application which use hashMap. But when I add additem
> > method like following added Item is not stored in a datastore.
>
> > public void addUsers(String userID, CustomUser user)
> > {
> > users.put(userID, user.)
>
> > }
>
> > I guess the reason of this problem come from that JDO recognize this
> > HashMap users as a serializable Blob. So when we access HashMap users,
> > JOD deserialize Blob and create HashMap instance and provide us as a
> > HashMap users instance. But JDO can not recognize what happen to the
> > users after deserialization. So JDO do not try to save users when I
> > close StateCore instance.
>
> > So I changed code as following. This code could store added
> > CoustomUser instance when I close StateCore instance.
>
> > @NotPersistent
> > private HashMap<String, CustomUser>usersTemp;
>
> > public void addUsers(String userID, CustomUser addUser)
> > {
> > users.put(userID, addUser.)
>
> > usersTemp=users;
> > users=null;
> > users=UsersTemp
>
> > }
>
> > Not sure this is the best counter measure for this problem but if
> > someone has same problem , please try.
--
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.