简体   繁体   English

GAE数据存储放置和获取

[英]GAE datastore put and get

Hi I'm just playing around with the datastore and hoping someone can tell my why my simple code doesn't work? 嗨,我只是在玩数据存储,并希望有人能告诉我为什么我的简单代码不起作用? I have just been playing around with how to put and get strings from the datastore... unfortunately my GAE ability is weak alongside my weak java ability... 我一直在研究如何从数据存储中放置和获取字符串...不幸的是,我的GAE能力很弱,而我的Java能力却很弱...

@SuppressWarnings("serial")
public class LoopingProgramServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {


    Key key = KeyFactory.createKey("a","b");        
    String Teststring = "pleasework";
    Entity greeting = new Entity("meh", key);
    greeting.setProperty("teststring", Teststring);     
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(greeting);



    Entity greetings = null;
    resp.setContentType("text/plain");
    resp.getWriter().println("doing something");


    try {
        greetings = datastore.get(key);
        String string = (String) greetings.getProperty("teststring");


        if (string == null) {
            resp.setContentType("text/plain");
            resp.getWriter().println("null");   
            System.out.println("null");
        } else {
            resp.setContentType("text/plain");
            resp.getWriter().println("contains something"); 
            System.out.println("contains something");
        }

        resp.setContentType("text/plain");
        resp.getWriter().println(string);   

    } catch (EntityNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }




}

} }

The Entity construct that you're using is interpreting the Key argument as the key to a parent entity. 您正在使用的Entity结构将Key参数解释为父实体的键。 Since no id or keyName are given (as would happen if you instead used one of the other Entity constructors described here ), an id will be generated for the greeting at put time. 由于没有给出id或keyName(如果您改为使用此处描述的其他Entity构造函数之一,则会发生这种情况),因此在put时将为问候语生成一个id。

datastore.get(key) is returning null because key refers to an entity of kind "a" with a keyName of "b", which (I'm guessing) doesn't exist. datastore.get(key)返回null,因为key指向类型为“ a”的实体,其keyName为“ b”,(我猜)这种实体不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM