开发者

How do I query a single field in AppEngine using JDO

开发者 https://www.devze.com 2023-03-27 12:02 出处:网络
I\'ve got a Product POJO that looks like. @PersistenceCapable(identityType = IdentityType.APPLICATION)

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(Boole开发者_如何转开发an 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消