JPA和app-engine中的子实体身份(危机)

时间:2013-04-05 16:15:40

标签: google-app-engine google-cloud-datastore datanucleus google-eclipse-plugin

我有两个模型:作者和书,作者可能有一个书籍列表。从作者开始有一个long @id,Book有一个字符串@id

当我尝试将数据放入数据存储区时,我收到错误Cannot have a java.lang.String primary key and be a child object (owning field …。在线搜索后,我登陆Persist List of objects。所以从那里我决定在Book类中使用

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

然后在构造函数中,我做

Key key = KeyFactory.createKey(Book.class.getSimpleName(), bookCode);
this.key = key; // which is the same as calling bookObject.setKey(key)

所以现在我收到了这个新错误

class Book has application-identity and no objectid-class specified yet has 0 primary key fields. Unable to use SingleFieldIdentity.

有人请提供一个可以解决此问题的代码段吗?或者至少是一些具体的指导?

编辑代码:

@Entity
public class Book{
   @PrimaryKey
   @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
   private Key key;

   private String title;

   //other fields

   //getters and setters follow

}

@Entity
public class Author{
   @Id
   private long ssn;

   private String firstName;

   private String lastName;

   List<Book> books;

   //getters and setters follow
}

编辑添加堆栈跟踪:

java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70)
    at com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:64)
    at com.friends.bookclub.dao.impl.GenericDaoImpl.getEntityManager(GenericDaoImpl.java:82)
    at com.friends.bookclub.dao.impl.GenericDaoImpl.put(GenericDaoImpl.java:45)
    at com.friends.bookclub.BookclubApi.registerUser(BookclubApi.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:304)
    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.DevAppServerServersFilter.doFilter(DevAppServerServersFilter.java:106)
    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:421)
    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)
Caused by: javax.persistence.PersistenceException: Explicit persistence provider error(s) occurred for "transactions-optional" after trying the following discovered implementations: org.datanucleus.api.jpa.PersistenceProviderImpl from provider: org.datanucleus.api.jpa.PersistenceProviderImpl
    at javax.persistence.Persistence.createPersistenceException(Persistence.java:242)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:184)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:70)
    at com.friends.bookclub.domain.utils.EMF.<clinit>(EMF.java:8)
    ... 50 more
Caused by: org.datanucleus.metadata.InvalidClassMetaDataException: Class com.friends.bookclub.db.Book has application-identity and no objectid-class specified yet has 0 primary key fields. Unable to use SingleFieldIdentity.
    at org.datanucleus.metadata.AbstractClassMetaData.determineObjectIdClass(AbstractClassMetaData.java:1355)
    at org.datanucleus.metadata.ClassMetaData.populate(ClassMetaData.java:209)
    at org.datanucleus.metadata.MetaDataManager$1.run(MetaDataManager.java:2699)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.datanucleus.metadata.MetaDataManager.populateAbstractClassMetaData(MetaDataManager.java:2693)
    at org.datanucleus.metadata.MetaDataManager.populateFileMetaData(MetaDataManager.java:2516)
    at org.datanucleus.metadata.MetaDataManager.initialiseFileMetaDataForUse(MetaDataManager.java:1123)
    at org.datanucleus.metadata.MetaDataManager.loadPersistenceUnit(MetaDataManager.java:986)
    at org.datanucleus.api.jpa.JPAEntityManagerFactory.initialiseNucleusContext(JPAEntityManagerFactory.java:754)
    at org.datanucleus.api.jpa.JPAEntityManagerFactory.initialise(JPAEntityManagerFactory.java:417)
    at org.datanucleus.api.jpa.JPAEntityManagerFactory.<init>(JPAEntityManagerFactory.java:380)
    at org.datanucleus.api.jpa.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:91)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:150)
    ... 52 more
Apr 5, 2013 8:54:21 AM com.google.api.server.spi.SystemService invokeServiceMethod
SEVERE: null
java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70)
    at com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:64)
    at com.friends.bookclub.dao.impl.GenericDaoImpl.getEntityManager(GenericDaoImpl.java:82)
    at com.friends.bookclub.dao.impl.GenericDaoImpl.put(GenericDaoImpl.java:45)
    at com.friends.bookclub.BookclubApi.registerUser(BookclubApi.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:304)
    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.DevAppServerServersFilter.doFilter(DevAppServerServersFilter.java:106)
    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:421)
    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)
Caused by: javax.persistence.PersistenceException: Explicit persistence provider error(s) occurred for "transactions-optional" after trying the following discovered implementations: org.datanucleus.api.jpa.PersistenceProviderImpl from provider: org.datanucleus.api.jpa.PersistenceProviderImpl
    at javax.persistence.Persistence.createPersistenceException(Persistence.java:242)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:184)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:70)
    at com.friends.bookclub.domain.utils.EMF.<clinit>(EMF.java:8)
    ... 50 more
Caused by: org.datanucleus.metadata.InvalidClassMetaDataException: Class com.friends.bookclub.db.Book has application-identity and no objectid-class specified yet has 0 primary key fields. Unable to use SingleFieldIdentity.
    at org.datanucleus.metadata.AbstractClassMetaData.determineObjectIdClass(AbstractClassMetaData.java:1355)
    at org.datanucleus.metadata.ClassMetaData.populate(ClassMetaData.java:209)
    at org.datanucleus.metadata.MetaDataManager$1.run(MetaDataManager.java:2699)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.datanucleus.metadata.MetaDataManager.populateAbstractClassMetaData(MetaDataManager.java:2693)
    at org.datanucleus.metadata.MetaDataManager.populateFileMetaData(MetaDataManager.java:2516)
    at org.datanucleus.metadata.MetaDataManager.initialiseFileMetaDataForUse(MetaDataManager.java:1123)
    at org.datanucleus.metadata.MetaDataManager.loadPersistenceUnit(MetaDataManager.java:986)
    at org.datanucleus.api.jpa.JPAEntityManagerFactory.initialiseNucleusContext(JPAEntityManagerFactory.java:754)
    at org.datanucleus.api.jpa.JPAEntityManagerFactory.initialise(JPAEntityManagerFactory.java:417)
    at org.datanucleus.api.jpa.JPAEntityManagerFactory.<init>(JPAEntityManagerFactory.java:380)
    at org.datanucleus.api.jpa.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:91)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:150)
    ... 52 more

2 个答案:

答案 0 :(得分:1)

我认为这个问题显然与书和作者的关键有关。

您可能不喜欢我的解决方案,可能不是最好的解决方案。至于我在GAE数据存储区处理自有关系的经验,我得到了许多与密钥相关的错误,所以我最终做了以下事情:

我将此作为所有我的班级的主键:

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

我让应用程序完全创建密钥,我的意思是,我不会尝试将任何应用程序创建的值添加到密钥中。我只是为密钥设置了getter和setter,但是我没有在构造函数中对它做任何事情。

如果我想为我的对象拥有自己的id / code / key,我只需在类中添加antoher属性。

我知道这可能是&#34;懦夫&#34;解决方案,但现在一切对我来说都更好......

编辑:如前所述,这不是最好的方法,因为关键查询比其他字段的查询更快,所以如果有人有一个好的解决方案,我也会很感激...

答案 1 :(得分:0)

我不知道这是否仍然相关,但它将解决作者类中的问题 -

 private long ssn;

Shoud be

 private Long ssn;