I asked question before asking if it is possible to save complex class composition in to the Google Datastore inside Google AppEngine with Java, but I was not clear enough and lazy to post all my class but after a lot of hours of struggle I start giving up. So here is more detailed question with the code.
I know this kind of stuff should work fine but for some unknown reason to me this is not working. The problem is that when I'm saving my object and close db connection then open it again the data is blank in that object. The object is in database, I can see it when I'm selecting ID of that object but everything else is blank. Unfortunately Google does not have database viewer to see what's in that database. I tried to search and ask for one but had no luck. (there is one but it's not working on my computer) So, here we go:
Content
// 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
}
Course
// 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();
}
}
Video
// 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
}
Document
// 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
}
Tag
// 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
}
Code to persist and get data from datastore:
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;
}
Here is the output:
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)
I know this error means that the string is null. But the record is in database. And it should be persist. Why is the data blank???
Any suggestions why this is happening to me?