gpt4 book ai didi

java - 从 Google Datastore 获取对象后,该对象为空

转载 作者:太空宇宙 更新时间:2023-11-04 08:59:40 25 4
gpt4 key购买 nike

asked question在询问是否可以使用 Java 将复杂的类组合保存到 Google AppEngine 内的 Google Datastore 之前,但我不够清楚,也懒得发布我的所有类,但经过很多小时的挣扎后,我开始放弃。所以这里是代码的更详细问题。

我知道这种东西应该可以正常工作,但由于某种未知的原因,我无法正常工作。问题是,当我保存对象并关闭数据库连接然后再次打开它时,该对象中的数据为空。该对象位于数据库中,当我选择该对象的 ID 时我可以看到它,但其他所有内容都是空白的。不幸的是,Google 没有数据库查看器来查看该数据库中的内容。我尝试搜索并 ask但没有运气。 (有 one 但它是 not working on my computer )那么,我们开始吧:

内容

// imports...
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Content{

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String id;

@Persistent protected String title;
@Persistent protected String thumbnailURL;
@Persistent protected List<Rating> ratings;
@Persistent protected List<Tag> tags;
@Persistent protected Double price;
@Persistent protected User owner;

// constructor and getters+setters
}

类(class)

// imports...
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Course extends Content{

@Persistent private Video video;
@Persistent private Document document;
@Persistent private String notes;
@Persistent private String summary;

// constructor and getters+setters
public String toString(){
return "ID: " + this.getId() + " Title: "+this.getTitle()+", Price: "+this.getPrice()+", No. of Tags: "+this.getTags().size();
}

}

视频

// imports...
public class Video extends Content {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
String id;

@Persistent String fileUrl;

// constructor and getters+setters
}

文档

// imports...
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Document extends Content /*AthenaObject*/ {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String id;

@Persistent private String docUrl;

// constructor and getters+setters
}

标签

// imports...
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Tag{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String id;

@Persistent private String tagText;

// constructor and getters+setters
}

用于保存数据并从数据存储区获取数据的代码:

public Boolean testCourse(){
Boolean isSaved = false;
PersistenceManager pm = PMF.get().getPersistenceManager();
Course c = new Course();
try{
List<Tag> tags = new ArrayList<Tag>();
tags.add(new Tag("tag1"));
tags.add(new Tag("tag2"));
tags.add(new Tag("tag3"));


c.setTitle("Course Title - " + new Date().getTime());
c.setPrice(99.90);
c.setTags(tags);

System.out.println(c.toString()); // **Output:** ID: null Title: Course Title - 1247116147858, Price: 99.9, No. of Tags: 3
pm.makePersistent(c);

Course cAfter = pm.getObjectById(Course.class, KeyFactory.stringToKey(c.getId()));
System.out.println(cAfter.toString()); // **Output:** agptYRtzaWL4gZDb3Vy4ErYFgw Title: Course Title - 1247116147858, Price: 99.9, No. of Tags: 3
isSaved = true;
}
catch(Exception e){
e.printStackTrace();
isSaved = false;
}
finally{
pm.close();
}


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

try{

Course cAfterClose = pm.getObjectById(Course.class, KeyFactory.stringToKey(c.getId()));
System.out.println(cAfterClose.toString()); // **Error**: See below
}
catch(Exception e){e.printStackTrace();}
finally{pm.close();}


return isSaved;
}

这是输出:

ID: nullTitle: Course Title - 1247117389679, Price: 99.9, No. of Tags: 3 ID: agptYWtzaW1zYXBwcgwLEgZDb3Vyc2UYGAwTitle: Course Title - 1247117389679, Price: 99.9, No. of Tags: 3 java.lang.NullPointerException at com.athena.server.entity.Course.toString(Course.java:94) at com.athena.server.CourseServiceImpl.testCourse(CourseServiceImpl.java:146) 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:585) at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:527) at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:166) at com.google.gwt.user.server.rpc.RemoteServiceServlet.doPost(RemoteServiceServlet.java:86) at javax.servlet.http.HttpServlet.service(HttpServlet.java:713) at javax.servlet.http.HttpServlet.service(HttpServlet.java:806) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093) at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405) at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:54) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139) at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:306) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139) at org.mortbay.jetty.Server.handle(Server.java:313) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:506) at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:844) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:644) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:381) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:396) at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:442)

我知道这个错误意味着字符串为空。但记录在数据库中。并且应该坚持下去。为什么数据为空???

有什么建议为什么会发生在我身上吗?

最佳答案

我不知道到底是什么导致了您的错误,但您可以尝试执行以下几个步骤来帮助排除故障:

首先,尝试将问题范围缩小到尽可能小的测试用例。您在上面发布了很多类(class),并且很可能仅使用其中的 2 个或 3 个类(class)即可重复此问题。

其次,如果您确实想准确查看实体在数据存储区中的样子,则必须执行类似的操作(或者您可以尝试将应用程序上传到 appengine 并在那里运行,这样您就可以使用他们的数据查看器):

Query q = pm.newQuery(Course.class);
List<Course> list = (List<Course>) q.execute();
for(Course c: list){
System.out.println("Course id:" + c.getId());
System.out.println("Course title:" + c.getTitle());
//...
}

关于java - 从 Google Datastore 获取对象后,该对象为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1101980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com