在Android客户端Google应用引擎的数据存储区中实现不一向的一对一关系

时间:2014-03-11 12:58:55

标签: java google-app-engine jpa google-cloud-datastore

我有两个参与者 - “UserMaster”和“FeedMaster”,他们有以下结构:

UserMaster

  • 用户id
  • 的userName

FeedMaster

  • feedId
  • userId_fk(指向UserMaster实体对象的外键)

为此我设计了实体类,如下所示。这些课程适合我的要求吗?

@Entity
public class FeedMaster {
@Id
    private String feedId;
@OneToOne
    @JoinColumn(name = "USER_ID")
    UserMaster user;

getter()
.........
setter()
}

@Entity
public class UserMaster {
@Id
private String userId;

    private String userName;

getter()
........
setter()
}

服务器错误日志:

com.google.api.server.spi.SystemService invokeServiceMethod: Error in meta-data for field com.sampleregistrationapp.UserMaster.userId : Cannot have a primary key of type java.lang.String and be a child object (owning field is "com.sampleregistrationapp.FeedMaster.user").
Error in meta-data for field com.sampleregistrationapp.UserMaster.userId : Cannot have a primary key of type java.lang.String and be a child object (owning field is "com.sampleregistrationapp.FeedMaster.user").
org.datanucleus.metadata.InvalidMetaDataException: Error in meta-data for field com.sampleregistrationapp.UserMaster.userId : Cannot have a primary key of type java.lang.String and be a child object (owning field is "com.sampleregistrationapp.FeedMaster.user").
    at com.google.appengine.datanucleus.MetaDataValidator.checkForIllegalChildField(MetaDataValidator.java:400)
    at com.google.appengine.datanucleus.MetaDataValidator.validateField(MetaDataValidator.java:292)
    at com.google.appengine.datanucleus.MetaDataValidator.validate(MetaDataValidator.java:200)
    at com.google.appengine.datanucleus.DatastoreManager.validateMetaDataForClass(DatastoreManager.java:571)
    at com.google.appengine.datanucleus.DatastorePersistenceHandler.fetchObject(DatastorePersistenceHandler.java:534)
    at org.datanucleus.state.JDOStateManager.loadFieldsFromDatastore(JDOStateManager.java:1638)
    at org.datanucleus.state.JDOStateManager.validate(JDOStateManager.java:3511)
    at org.datanucleus.ObjectManagerImpl.findObject(ObjectManagerImpl.java:3379)
    at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:358)
    at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:256)
    at com.sampleregistrationapp.FeedMasterEndpoint.containsFeedMaster(FeedMasterEndpoint.java:161)
    at com.sampleregistrationapp.FeedMasterEndpoint.insertFeedMaster(FeedMasterEndpoint.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:45)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:359)
    at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:124)
    at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:82)
    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 org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    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 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.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
    at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:188)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
    at java.lang.Thread.run(Thread.java:724)

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我开始知道如果我们在另一个实体中将任何对象定义为子对象,那么该子实体必须使用Key类来设置其标识列。所以我改变了我的编码屁股跟随,现在它正常工作。我在Android客户端使用它。

UserMaster:

@Entity
public class UserMaster {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;    
    private String userName;

    getter()...setter()
}

FeedMaster:

@Entity
public class FeedMaster {
    @Id
    private String feedId;
    @OneToOne(cascade = CascadeType.ALL)
    UserMaster user;

    getter()...setter()
}

Android客户端:

Usermasterendpoint.Builder userBuilder = new Usermasterendpoint.Builder(
AndroidHttp.newCompatibleTransport(),new JacksonFactory(), new HttpRequestInitializer()
                        {
                public void initialize(HttpRequest arg0)throws IOException {
                }
                });
Usermasterendpoint userEndpoint = CloudEndpointUtils.updateBuilder(userBuilder).build();
UserMaster user = userEndpoint.getUserByName(userID).execute();
Feedmasterendpoint.Builder builder = new Feedmasterendpoint.Builder(
      AndroidHttp.newCompatibleTransport(), new JacksonFactory(), 
      new HttpRequestInitializer() {
            public void initialize(HttpRequest arg0)throws IOException {
            }
      });
Feedmasterendpoint endpoint = CloudEndpointUtils.updateBuilder(builder).build();
FeedMaster objFeed = new FeedMaster();
objFeed.setFeedId(userID + new Data().toString());
objFeed.setUser(user);
endpoint.insertFeedMaster(objFeed).execute();