在没有PersistantManager的GAE DataStore中从实体获取特定值

时间:2014-06-05 06:34:45

标签: java google-app-engine validation google-cloud-datastore

我试图注册用户并允许他使用userName和Password登录。我的dataStore实体" Racer"包含名称,用户名,密码,年龄,BikeModel,城市。密钥由应用程序生成。

我想验证用户。我使用addFilter()查询来获取特定用户的全部细节。我很震惊从dataStore中的实体获取特定用户的特定属性,比如来自" Racer"的特定用户的密码和用户名。

在网上,无论我得到什么样的例子,都是使用PersistentManager。我不想使用它。我没有找到任何查询来获取除筛选和排序之外的详细信息。请建议我如何继续。

的Servlet

  public class StoreInDataStore extends HttpServlet {
@SuppressWarnings("deprecation")
public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {

    //Getting SignUp form values
        String name=req.getParameter("name1");
     String uname=req.getParameter("uname1");
     String pswd=req.getParameter("pswd1");
     String age=req.getParameter("age");
     String city=req.getParameter("city");
     String bike=req.getParameter("bike");

    //Getting login form values
     String lname=req.getParameter("name");
     String lpswd=req.getParameter("pswd");

     //Creating dataStore
     DatastoreService ds = DatastoreServiceFactory.getDatastoreService();

     //Creating entity
     Entity e=new Entity("Racer");

     //Entering details in racer
     e.setProperty("Name", name);
     e.setProperty("UserName", uname);
         e.setProperty("Password", pswd);
     e.setProperty("Age", age);
     e.setProperty("City", city);
     e.setProperty("Bike Model", bike);

    //Storing in DataStore  
        ds.put(e);      

            //Getting entire details

        Query q=new Query("Racer");
    q.setFilter(FilterOperator.EQUAL.of("UserName", uname));/* UserName takes only the currently entered value(uname). Doesnot fetch from dataStore */
    PreparedQuery pq=ds.prepare(q);
    List<Entity> result = ds.prepare(q).asList(FetchOptions.Builder.withDefaults());

    System.out.println(result);     

}

1 个答案:

答案 0 :(得分:0)

而不是

List<Entity> result = ds.prepare(q).asList(FetchOptions.Builder.withDefaults());

使用

Entity result = ds.prepare(q).asSingleEntity();
String password = result.getProperty("Password");