How do I query a single field in AppEngine using JDO

江枫思渺然 提交于 2020-01-15 08:30:28

问题


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


回答1:


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.




回答2:


Entities are stored as serialized blobs of data in the datastore, so it's not possible to retrieve and return a single field from an entity. You need to fetch the whole entity, and extract the field you care about yourself.



来源:https://stackoverflow.com/questions/7020082/how-do-i-query-a-single-field-in-appengine-using-jdo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!