Refreshing an entity with JDO and GAE

So I am creating a small web application using Wicket that will run in the google engine. I am using JDO and so far there is no problem persisting data (inserts) or querying data with the same data objects. Maybe I am missing basic things, I am trying to take one of these objects by changing two values, and then I want the changed object to be updated in the data store.

I have a User object that is being persisted. I can ask for this, so I know the object itself has the correct annotations.

The code I'm running to update it:

final PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction trans = pm.currentTransaction();
try{ 
  trans.begin();
  user.setLoginCount(user.getLoginCount()+1);
  user.setLastLoginTime(new Date());
  trans.commit();
}finally{
  if (trans.isActive()) {
    trans.rollback();
    System.out.println("----transaction rollback - submitLogin----");
  }
  pm.close();
}

      

Here's my user object annotations for these two things I want to change ...

@Persistent
private Date lastLoginTime;

@Persistent
private int loginCount = 0;

      

I make a request right after this code and it returns the same object before I changed the values.
Any ideas what I am doing wrong?

+2


source to share


1 answer


By default, JDOs are only valid when you open the PersistenceManager it creates. You say that you have already called makePersistent () on the user object. This means that you have opened another PersistenceManager before the one you show us in the above code. When you closed this PersistenceManager (presumably you closed it), your object is invalidated (unofficially).

Depending on what you want to do, you have two options.

  • You can detach your custom object, which will allow it to have life outside of the context of its original save manager.

  • You can get a fresh copy of the entity using the newly created PersistenceManager



here is the code for option # 2:

trans.begin();
User user = (User) pm.getObjectById(User.class,"userkeyhere");
user.setLoginCount(user.getLoginCount()+1);
user.setLastLoginTime(new Date());
trans.commit();

      

+2


source







All Articles