java—在android客户端google app engine的数据存储中实现无向一对一关系

mbzjlibv  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(293)

我有两个实体-“usermaster”和“feedmaster”,它们有以下结构:
用户主控
用户ID
用户名
feedmaster公司
饲料ID
userid\u 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)
8i9zcol2

8i9zcol21#

我解决了这个问题。我知道,如果我们将任何对象定义为另一个实体中的子对象,那么该子实体必须使用 Key 类来设置其标识列。所以我改变了我的代码,现在它可以正常工作了,我是从android客户端使用它的。
用户主机:

@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();

相关问题