如何通过键选择单个实体(低级java数据存储区API) [英] How to select a single Entity by key (low level java datastore API)

查看:100
本文介绍了如何通过键选择单个实体(低级java数据存储区API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何创建一个Key对象来选择我的实体(Customer)的一行有点困惑。



我的代码:

 查询查询=新查询(客户); 
// ****我该如何创建这个键?
Key key = KeyFactory.createKey(Customer,1);
// ****
FilterPredicate keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY,FilterOperator.EQUAL,key);
query.setFilter(keyFilter);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
PreparedQuery pq = datastore.prepare(query);
实体客户= pq.asSingleEntity();
if(!(customer == null)){
log.info(OK!);
}
else {
log.info(找不到客户);
}

我的结果总是:找不到客户。



使用数据存储区查看器(我在本地工作)可以看到3行:


  • id / name = 1 email =your emailname =your name

  • id / name = 2 email =your emailname =your name $ b
  • id / name = 3 email =您的电子邮件地址name =您的姓名



id / name = 1。
我试过了:
KeyFactory.createKey(Customer,1);

KeyFactory.createKey(Customer,your name);

但没有成功。



当我以编程方式在Customer上搜索(asList)并打印出键值时,我看到:


  • 第1行key =客户(您的姓名)/客户(1)

  • 第2行key =客户
  • row 3 key = Customer(your name)/ Customer(3)



可打印的值这些键的格式为:实体(名称)/实体(ID /名称)

如何在我的代码中创建此类键值?
在javadoc上我只看到:
$ b $ ul
createKey(kind,id)

  • createKey(kind,name)



  • 其他方法需要祖先,我没有创建祖先。 ...
    这是我创建3行的代码(已执行3次):

      Key customerKey = KeyFactory.createKey(Customer,your name); 
    实体客户=新实体(客户,customerKey);
    customer.setProperty(name,your name);
    customer.setProperty(email,你的电子邮件);
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(customer);

    致谢!



    / p>

    最大值

    换句话说,如何创建一个其toString给出以下结果的Key ?




    • 代码:log.info(customer KeyFactory.keyToString =+ KeyFactory.keyToString(key)); / li>
    • 输出:INFO:customer KeyFactory.keyToString = Customer(your name)/ Customer(1)

      =h2_lin>解决方案

      实体的创建不正确。
      您不必自己创建密钥。
      如果您使用kind和keyname或ID参数创建一个实体,则会自动从该种类和键名或ID中创建一个键。

       实体客户=新实体(客户,您的姓名); 
      customer.setProperty(email,你的电子邮件);

      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
      datastore.put(customer);

      这就是您需要创建所需实体的全部功能。



      当您给出一种和一个键时,将创建一个子实体。
      因此,您创建了一个带有您的姓名的客户,他是ID为1的客户。



      要使用密钥获取实体,您只需要使用相同的参数来创建一个键。

        Key key = KeyFactory.createKey(Customer,your name); 

      我宁愿使用get(),但我不知道你想要做什么。
      举例来获取客户的电子邮件:

       实体e; 
      尝试{
      e = datastore.get(key);

      String myEmail =(String)e.getProperty(your email);


      I'm a bit confused on how to create a "Key" object to select exactly 1 row of my Entity ("Customer").

      My code :

      Query query = new Query("Customer");
      // **** how do I have to create this key ???
      Key key = KeyFactory.createKey("Customer", 1);
      // ****
      FilterPredicate keyFilter =  new FilterPredicate(Entity.KEY_RESERVED_PROPERTY,       FilterOperator.EQUAL, key);
      query.setFilter(keyFilter);
      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
      PreparedQuery pq = datastore.prepare(query);
      Entity customer = pq.asSingleEntity();
      if  (! (customer == null)) {
          log.info("OK !");
      }
      else {
          log.info("no customer found");
      }
      

      My result is always : "no customer found".

      With the datastore viewer (I'm working locally) I can see 3 rows :

      • id/name = 1 email = "your email" name = "your name"
      • id/name = 2 email = "your email" name = "your name"
      • id/name = 3 email = "your email" name = "your name"

      I want to select customer with id/name=1. I've tried : KeyFactory.createKey("Customer", 1); and KeyFactory.createKey("Customer", "your name");

      but with no success.

      When I do programmatically a search (asList) on "Customer" and I print the keys' values I see :

      • row 1 key = Customer("your name")/Customer(1)
      • row 2 key = Customer("your name")/Customer(2)
      • row 3 key = Customer("your name")/Customer(3)

      The printable values of the keys are in the format : "Entity(name)/Entity(id/name)"

      How can I create such key values in my code ? On the javadoc I see only :

      • createKey("kind", id)
      • createKey("kind", "name")

      The other methods require an ancestor, I've not created an ancestor .... This is the code that I've executed to created the 3 rows (it has been executed 3 times) :

      Key customerKey = KeyFactory.createKey("Customer", "your name");
      Entity customer = new Entity("Customer", customerKey);
      customer.setProperty("name", "your name");
      customer.setProperty("email", "your email");
      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
      datastore.put(customer);
      

      Thanks in advance !

      Regards

      Max

      In other words, how do create a Key whose "toString" gives the following result ?

      • Code : log.info("customer KeyFactory.keyToString = "+KeyFactory.keyToString(key));
      • Output : INFO: customer KeyFactory.keyToString = Customer("your name")/Customer(1)

      解决方案

      The creation of the Entities is not correct. You dont have to create a key on your own. If you create a Entity with a kind and keyname or ID parameter, a key will be created automaticlly out of the kind and the keyname or ID.

      Entity customer = new Entity("Customer", "your name");
      customer.setProperty("email", "your email");
      
      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
      datastore.put(customer);
      

      That is all you need to create the Entity you want.

      When you give a kind and a key, a child Entity will be created. So you had created a Customer with "your name" who is a Customer with the ID 1.

      To get the Entity with a key you just have to take the same parameters to create a key.

      Key key = KeyFactory.createKey("Customer", "your name");
      

      I would prefer to use get(), but i dont know what you want to do. For Example to get the email of a Customer:

          Entity e;
          try {
              e = datastore.get(key);
      
              String myEmail= (String) e.getProperty("your email");
      

      这篇关于如何通过键选择单个实体(低级java数据存储区API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆