1

I'm getting the following error using GAE, JPA, and Spring

Object with id “” is managed by a different Object Manager

When I first create an account, I put the User object in the session. Then when I update the user profile during that initial session, I merge the detached User. All works great.

I then logout and later create a new session. This time, I load the User object and place into the session. Still OK, but problem is when I update the user profile, the merge fails with the above error.

public boolean loadProfile(String openId, String email) {
    User user = null;
    try {
        user = userDao.findByOpenId(openId);
    } catch (NoResultException e) {
    }
    if (user != null) {
        logger.error(JDOHelper.getPersistenceManager(user));
        getSessionBean().setUser(user);
        return true;
    } else {
        user = createNewAccount(openId, email);
        getSessionBean().setUser(user);
        return false;
    }

}

@Transactional(propagation=Propagation.REQUIRES_NEW)
private User createNewAccount(String openId, String email) {
    User user = new User();
    user.setDisplayName(Long.toString(System.currentTimeMillis()));
    OpenIdentifier oid = new OpenIdentifier();
    oid.setOpenId(openId);
    oid.setEmail(email);
    oid.setUser(user);
    Set<OpenIdentifier> openIds = new HashSet<OpenIdentifier>();
    openIds.add(oid);
    user.setOpenIds(openIds);
    user = userDao.merge(user);
    return user;
}

@Transactional(propagation=Propagation.REQUIRED)
public void createOrUpdate(ActionEvent e) {
    logger.error(JDOHelper.getPersistenceManager(userFacade.getDelegate()));
    User user = userDao.merge(userFacade.getDelegate());
    sessionBean.setUser(user);
}

I found these related questions, but I'm still not able to fix.

  1. AppEngine datastore: "Object with id ... is managed by a different Object Manager"
  2. Google App Engine - Object with id "" is managed by a different - JPA
  3. Datanucleus: moving from @Transactional to non-transactional
  4. http://www.atentia.net/2010/03/object-with-id-is-managed-by-a-different-object-manager/

WRT closing the PM (as per 1 & 2), I'm not able to explicitly close the PM since I'm using Spring org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter. From logs, it appears to be opening and closing on each page request.

WRT making the entity detachable (as per 3 & 4), first of all, I'm using JPA and it seems wrong to use a JDO-related annotation. Secondly, it didn't work when I tried.

For extra credit, how do you debug with JDOHelper.getPersistenceManager(obj)? I am getting null in this case, as the User was detached between page requests. That seems normal to me so I'm not clear how to debug with it.

Community
  • 1
  • 1
s_t_e_v_e
  • 2,496
  • 3
  • 31
  • 35

1 Answers1

1
  1. You don't have a PM, you have an EM. No idea what you're referring to there.
  2. Detachable : with JPA all classes are (enhanced as) detachable
  3. You're using some ancient GAE JPA plugin there (v1.x?), and that uses old versions of DataNucleus that are not supported. Use GAE JPA v2.x. "ObjectManager" hasn't existed in DataNucleus for years.
  4. You (or the software you're using) have to close the EM or you get resources leaked all over the place.

NucleusJPAHelper.getEntityManager(obj); is how you get the EntityManager that manages an object (in DataNucleus v3.x, used by GAE JPA v2.x)

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
  • Yes, I'm sure it was due to a hodge-podge of dependencies that I copy/pasted when I initially set it up. I even found a few JDO annotations sprinkled in with mostly JPA code. I also realized that had been using the DN enhancer defaulted to JDO. Somehow, it was mostly working. – s_t_e_v_e Apr 14 '13 at 01:55
  • In any case, I upgraded to JPA v2.x/DataNucleus v3.x and removed JDO references. Don't know whether this fixed it yet though, since I'm getting "javax.persistence.PersistenceException: No meta data for mypackage.OpenIdentifier. Perhaps you need to run the enhancer on this class?". The logs show the enhancer is running and the classes are updated in place in the target directory. – s_t_e_v_e Apr 14 '13 at 01:56