我想为我的应用程序引擎应用程序实现一对多的双向关系。下面是我的实体:
@Entity
public class UserMaster {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
private String userName;
@OneToMany(mappedBy = "user")
List<FeedMaster> feeds;
getter()...setter() for all properties
}
@Entity
public class FeedMaster {
@Id
private String feedId;
@ManyToOne(fetch = FetchType.LAZY)
UserMaster user;
getter()....setter() for both properties
}
在这里,
UserMaster
的第一个对象被持久化,然后当我尝试persisFeedMaster
的对象时,它抛出java.io.IOException: com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException
异常。错误日志:
Uncaught exception from servlet
java.io.IOException: com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.sampleregistrationapp.FeedMaster["user"]->com.sampleregistrationapp.UserMaster["key"]->com.google.appengine.api.datastore.Key["appId"])
at com.google.api.server.spi.response.ServletResponseResultWriter.writeValueAsString(ServletResponseResultWriter.java:187)
at com.google.api.server.spi.response.ServletResponseResultWriter.write(ServletResponseResultWriter.java:73)
at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:386)
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 com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60)
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 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.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:266)
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 com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:446)
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 com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)
................................
我找了很多,但找不到任何解决办法。我提到了stackoverflow link,但在这里,他们说要删除
UserMaster
中FeedMaster
的getter setter,但是如果我删除了它,我将无法通过我的UserMaster
对象访问FeedMaster
数据。请帮我解决这个问题。谢谢您。 最佳答案
这里发生的事情是jackson库正在寻找一个不能转换为json的循环。在返回此对象时,必须决定要返回关系的位置,以及不返回关系的位置。
您的代码意味着您可以从FeedMaster
访问UserMaster
,从UserMaster
访问FeedMaster
。在您的应用程序级别,这是非常好的,但是当您将这个对象图序列化为json时,您将不得不放弃其中一个关系,因为它将导致json表示的无休止循环。
为此,您可以使用@JsonIgnore
注释,如下所示:
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
public class FeedMaster {
@Id
private String feedId;
@ManyToOne(fetch = FetchType.LAZY)
UserMaster user;
@JsonIgnore
public UserMaster getUser() {
...
}
}
在这种情况下,json将允许您访问
FeedMaster
中的所有UserMaster
对象,但不能访问UserMaster
中的FeedMaster
对象。