如何使用JDO在AppEngine中查询单个字段 [英] How do I query a single field in AppEngine using JDO

查看:108
本文介绍了如何使用JDO在AppEngine中查询单个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Product extends AbstractModel {
@Persistent
private String name;

@Persistent
私人钥匙homePage;

@Persistent
私有布尔特征;

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public Key getHomePage(){
return homePage;
}

public void setHomePage(Key homePage){
this.homePage = homePage;
}

public boolean isFeatured(){
return featured;
}

public void setFeatured(布尔特色){
this.featured = featured;
}
}

我的DataStore目前完全是空的。



我想要检索所有homePage关键字,其中的产品功能都是正确的。

我正在尝试

  PersistenceManager persistenceManager = getPersistenceManager(); 
Query query = persistenceManager.newQuery(SELECT homePage FROM+ getModelClass());
query.setFilter(featured == true);

列表< Key> productPageKeys =(List< Key>)query.execute();

然而,这给了我一个空指针错误。我应该如何构建这个查询?



干杯,
Peter

解决方案 div>

为了做一个投影,你可以做一些类似的事情:

  Query q = pm.newQuery(SELECT myField FROM mydomain.MyClass WHERE featured == true); 
列表< String> results =(List< String>)q.execute();

其中String是我的字段的类型。任何基本的JDO文档都会定义它。
内部GAE / J将检索实体,然后在将其返回给用户之前进行后处理,然后将其处理为您需要的投影。



正如Nick在其他回复中指出的那样,这并不会带来性能上的好处,但是,标准持久性API的全部内容就是要保护您免受此类数据存储的特​​定情况的影响,它们都是开箱即用的。


I've got a Product POJO that looks like.

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Product extends AbstractModel {
    @Persistent
    private String name;

    @Persistent
    private Key homePage;

    @Persistent
    private Boolean featured;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Key getHomePage() {
        return homePage;
    }

    public void setHomePage(Key homePage) {
        this.homePage = homePage;
    }

    public boolean isFeatured() {
        return featured;
    }

    public void setFeatured(Boolean featured) {
        this.featured = featured;
    }
}

My DataStore is currently completely empty.

I'd like to retrieve all homePage keys where featured is true for the Product.

I'm trying

PersistenceManager persistenceManager = getPersistenceManager();
Query query = persistenceManager.newQuery("SELECT homePage FROM " + getModelClass());
query.setFilter("featured == true");

List<Key> productPageKeys = (List<Key>) query.execute();

However this is giving me a null pointer error. How should I be constructing this query?

Cheers, Peter

解决方案

To do a projection, you would do something like

Query q = pm.newQuery("SELECT myField FROM mydomain.MyClass WHERE featured == true");
List<String> results = (List<String>)q.execute();

where String is the type of my field. Any basic JDO documentation would define that. Internally GAE/J will retrieve the Entity, and then in the post-processing before returning it to the user it is manipulated into the projection you require.

As Nick pointed out in the other reply, this gives no performance gain over doing it yourself ... but then the whole point of a standard persistence API is to shield you from such datastore-specifics of having to do such extraction; it's all provided out of the box.

这篇关于如何使用JDO在AppEngine中查询单个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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