I would like to create an application that saves and retrieves records to the GAE server. I followed the tutorial "Deploying to Google App Engine" http://code.google.com/webtoolkit/doc/latest/tutorial/appengine.html to get started.
I have the StockWatcher application working now, but in my application I need to store a String that can be large (>10KB). I read that I can't use a Java String type to store large strings and need to use the Text data type instead.
I think by Text, they mean: com.google.appengine.api.datastore.Text, but it would be nice to confirm this is correct. ???
Regardless, I can't get Text to work. After some research it appears both the types Key and Text can only be used in the server code and not the client code. It seems that this is because the source code is not available for these classes and GWT needs the source to create the JavaScript code on the client's computer. At least that my current working hypothesis as to why I'm getting the following errors:
21:52:52.823 [ERROR] [myapp] Line 15: The import com.google.appengine.api.datastore cannot be resolved
21:52:52.951 [ERROR] [myapp] Line 103: Key cannot be resolved to a type
21:52:53.011 [ERROR] [myapp] Line 106: Text cannot be resolved to a type
I use the following fields in a class in a shared folder.
shared/MyDataRecord
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;
@Persistent
private Text description;
MyDataRecord class in a shared folder because I wanted to use to send back all the fields in one get method return rather than multiple individual field get methods. Here's how I use MyDataRecord class开发者_如何学Python in my server/DataServiceImpl.java class
public class DataServiceImpl extends RemoteServiceServlet implements DataService
{
...
@Override
public MyDataRecord getDataRecord() throws NotLoggedInException
{
...
I've seen some posted solutions suggest using non-standard, 3rd party libraries, like http://www.resmarksystems.com/code/. I couldn't get this one installed, but even if I could, I'd prefer a different solution. Storing Text must be such a common task that I'd prefer to solve this using what is considered a standard solution.
I could change my code to return each field in multiple get methods instead of an single return of a MyDataRecord instance. However, even if that works, that would be significantly more work and more difficult to maintain over time. However, if this is what is normally expected, then that's what I'll do.
I'd like to solve this using what is considered best practices by GWT and GAE. A simple example or tutorial would go a long way, but I can't find one.
Are there example programs/tutorials that show what GWT considers best practices for storing and retrieving large strings?
I am a newbie with both GWT and GAE (as well as web development), please consider this in any responses, thanks.
No Snark Please
The serializable POJO. Note the NotPersistent annotation for description
package com.my.project.shared;
@PersistenceCapable(identityType=IdentityType.APPLICATION,detachable="true")
public class MyParent implements Serializable {
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Long id;
@NotPersistent //Note the NotPersistent annotation. GAE won't persist this value in big table
private String description;
}
The second POJO. Notice the package
package com.my.project.server;
@PersistenceCapable(identityType=IdentityType.APPLICATION,detachable="true")
public class MyChild implements Serializable{//Not really required to implement Serializable
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private Long parentID;//Reference to the MyParent
@Persistent
private Text description;//The actual value of the description variable.
}
Notice the parent ID mapped in the child. While retrieving you will need to identify which child belongs to which parent. In pseudo code 1) Load parent from DB 2) Identify child for this parent, and load it 3) Convert child.description->parent.description 4) Now you have a fully constructed parent POJO which is serializable. Send it to the UI
Just reverse the procedure on the way back from UI to GAE.
1) Define a NotPersistent field in your serializable POJO private String description 2) Define a new POJO on the server side which will have private Text description 3) When you persist/load the original POJO, retrieve the new POJO and populate the String description from the Text description
精彩评论