我正在使用JDO在Google App Engine中创建一个Web应用程序,并得到一个JDOFatalUserException

javax.jdo.JDOFatalUserException: Detected attempt to establish User(15) as the
parent of SessionToken("VALID_USER_TOKEN") but the entity identified
by SessionToken("VALID_USER_TOKEN") has already been persisted
without a parent.  A parent cannot be established or changed once an
object has been persisted.


我有User类,其中有一个SessionToken。我想在数据存储区中同时存储User和SessionToken。用户密钥是自动生成的,而SessionToken密钥是由我生成的。

@PersistenceCapable
public class User {

  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key;
  @Persistent
  private SessionToken sessionToken;

  //getters and setters...
}


和:

@PersistenceCapable
public class SessionToken {

  @PrimaryKey
  private Key token;

  public SessionToken(Key token) {
    this.token = token;
  }

  //getters and setters...
}


然后,我想创建一个用户,所以我这样做:

PersistenceManager pm = PMF.get().getPersistenceManager();

User user = new User();

Key key = KeyFactory.createKey(SessionToken.class.getSimpleName(),
    "VALID_USER_TOKEN");
user.setSessionToken(new SessionToken(key));

try {
  pm.makePersistent(user);
} finally {
  pm.close();
}


这是完整的异常转储:

javax.jdo.JDOFatalUserException: Detected attempt to establish User(15) as the parent of SessionToken("VALID_USER_TOKEN") but the entity identified by SessionToken("VALID_USER_TOKEN") has already been persisted without a parent.  A parent cannot be established or changed once an object has been persisted.
    at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:498)
    at org.datanucleus.api.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:736)
    at org.datanucleus.api.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:756)
    at com.mockgaeapp.UsersEndpoint.register(UsersEndpoint.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:239)
    at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:161)
    at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:120)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:61)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:409)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)


我不明白该错误,因为在保存用户之前我不保存SessionToken对象。

此异常是什么意思?

最佳答案

您的代码告诉JDO在UserSessionToken对象之间具有“拥有”关系,在这种情况下,子实体应该以父项的键作为其父项(在数据存储区中),而您明确设置了键值而没有父项。

请尝试使用'unowned' relationship

09-25 18:13