如何通过id(Long值)检索Google Appengine对象? [英] How to retrieve Google Appengine Objects by id (Long value)?

查看:125
本文介绍了如何通过id(Long值)检索Google Appengine对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过以下方式声明了一个实体:

  public class MyEntity {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
私人长ID;
@Persistent
私人字符串文本;
// getters and setters
}

现在我想要检索对象使用ID。我尝试从Google Appengine数据查看器中通过SELECT * FROM MyEntity Where id = 382005或通过servlet中的查询进行管理。我没有得到任何结果。但我知道肯定与id的对象存在(我做了一个jsp查询数据库中的所有对象,并显示在数据库中)。

那么我的查询出了什么问题?我是否查询错误的字段? Google Appengine Data Viewer将字段ID /名称命名为id = 382005。我必须用这个名字来查询吗?我已经尝试过,但没有成功:($ / b>

解决方案

您可以在下面使用,因为您使用主键进行查询:

  MyEntity yourEntity = entityManager.find(MyEntity.class,yourId); 

请注意,这应该可以工作,但是如果您基于主键进行搜索,则使用find()会更容易:

  Query query = entityManager.createQuery(
SELECT m FROM MyEntity m WHERE id =:id);
query.setParameter (id,yourId);

MyEntity yourEntity =(MyEntity)query.getSingleResult();


i have declared an entity the following way:

public class MyEntity {
 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 private Long id;
 @Persistent
 private String text;
        //getters and setters
}

Now I want to retrieve the objects using the id. I tried to manage it from the Google Appengine Data Viewer with "SELECT * FROM MyEntity Where id = 382005" or via a query in a servlet. I get no results returned. But i know for sure that the object with the id exists (i made a jsp which queries all objects in the db and displays them in the db).

So what is wrong in my query? Am I querying the wrong field? The Google Appengine Data Viewer names the field "ID/name" and it has the value "id=382005". Do I have to query with this names? I've tried but it didn't work out :(

解决方案

You can use below since you are querying using the primary key:

MyEntity yourEntity = entityManager.find(MyEntity.class, yourId);

Note, this should work as well, but it's easier to use find() if you are searching based on the primary key:

Query query = entityManager.createQuery(
    "SELECT m FROM MyEntity m WHERE id = :id");
query.setParameter("id", yourId);

MyEntity yourEntity = (MyEntity) query.getSingleResult();

这篇关于如何通过id(Long值)检索Google Appengine对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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