`
iamxiaole
  • 浏览: 19297 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Part4 - JDO/JPA执行Keys-Only的查询

阅读更多
Welcome to Episode 4 of JDO/JPA Snippets That Work.  Today we're going to
look at keys-only queries.

Keys Only Queries

If you use the low-level datastore api you may have noticed that the
com.google.appengine.api.datastore.Query class has a setKeysOnly() method.
If you call this method before you execute the query the datastore will
return com.google.appengine.api.datastore.Entity instances that have their
keys filled in but none of their properties.  This can reduce consumption of
your Datastore Data Received from API quota, especially if you've got some
large entities, but, more importantly, it can also reduce consumption of
your Datastore CPU Time quota.  How?  Well, if the fulfillment of your query
requires an index or a merge join your query actually executes in two
stages:  First it scans the index to find the keys of the entities that
match and then it issues additional scans to retrieve the entities uniquely
identified by the matching keys.  If your query is keys-only we can skip
that second step entirely.  That means faster queries!
Now, JPA and JDO don't know anything about keys-only queries, but they do
give you the flexibility to either return your entire object or some subset
of the fields on your object.  If you construct this subset to only contain
the primary key of your object, the App Engine implementation of JPA and JDO
will use a keys-only query.  Let's look at some examples:

JPA:
@Entity 
public class Book { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Key id; 
    private Date dateOfPublication; 
    // getters and setters 
} 


Now let's implement a method that returns the Keys of all Books published
betweeen 2 years (we'll assume someone else is creating and closing an
EntityManager named 'em' for us):

public List<Key> keysOfBooksPublishedBetween(EntityManager em, Date from, 
Date to) { 
    em.getTransaction().begin(); 
    try { 
        Query q = em.createQuery("select id from " + Book.class.getName() 
            + " where dateOfPublication >= :from AND dateOfPublication <= 
:to"); 
        q.setParameter("from", from); 
        q.setParameter("to", to); 
        return (List<Key>) q.getResultList(); 
    } finally { 
        em.getTransaction().rollback(); 
    } 
} 


JDO:
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = 
"true") 
public class Book { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key id; 
    private Date dateOfPublication; 
    // getters and setters 
}


Now let's implement a method that returns the Keys of all Books published
betweeen 2 years (we'll assume someone else is creating and closing a
PersistenceManager named 'pm' for us):

public List<Key> keysOfBooksPublishedBetween(PersistenceManager pm, Date 
from, Date to) { 
    pm.currentTransaction().begin(); 
    try { 
        Query q = pm.newQuery("select id from " + Book.class.getName() 
            + " where dateOfPublication >= :from && dateOfPublication <= 
:to"); 
        return (List<Key>) q.execute(from, to); 
    } finally { 
        pm.currentTransaction().rollback(); 
    } 
} 


--------------------------------
Notice how we are only selecting the 'id' field from our Book class.  This
is crucial.  If you select any other fields your query will end up fetching
entire entities and the optimization will be lost.
Max

转载自:http://groups.google.com/group/google-appengine-java/browse_thread/thread/1d699d0db8641dbd#
分享到:
评论

相关推荐

    datanucleus-api-jdo-3.2.6-API文档-中文版.zip

    赠送jar包:datanucleus-api-jdo-3.2.6.jar; 赠送原API文档:datanucleus-api-jdo-3.2.6-javadoc.jar; 赠送源代码:datanucleus-api-jdo-3.2.6-sources.jar; 赠送Maven依赖信息文件:datanucleus-api-jdo-3.2.6....

    datanucleus-api-jdo-4.2.1-API文档-中英对照版.zip

    赠送jar包:datanucleus-api-jdo-4.2.1.jar; 赠送原API文档:datanucleus-api-jdo-4.2.1-javadoc.jar; 赠送源代码:datanucleus-api-jdo-4.2.1-sources.jar; 赠送Maven依赖信息文件:datanucleus-api-jdo-4.2.1....

    datanucleus-api-jdo-3.2.6.jar )

    datanucleus-api-jdo-3.2.6.jar )

    ssh+mysql55jar包集合

    /xscjManager/WebContent/WEB-INF/lib/spring-jdo.jar /xscjManager/WebContent/WEB-INF/lib/spring-jms.jar /xscjManager/WebContent/WEB-INF/lib/spring-jmx.jar /xscjManager/WebContent/WEB-INF/lib/spring-jpa....

    spring-jdo.jar

    对JDO 1.0/2.0的支持。外部依赖spring-jdbc, JDO API, (spring-web)。

    xdoclet-plugin-jdo-1.0.3.zip

    xdoclet-plugin-jdo-1.0.3正是为了将XDoclet的强大自动化功能应用于JDO开发,它允许开发者在Java类的注释中定义JDO元数据,如持久化字段、查询、事务属性等。这些注释会被XDoclet解析,并自动生成相应的JDO配置文件...

    DataNucleus JPA/JDO访问mysql示例

    5. **查询语言**:了解DataNucleus支持的JPA的JPQL和JDO的QOM(Query Object Model),以及如何执行复杂的查询。 6. **性能优化**:学习如何利用DataNucleus的缓存机制和优化选项提高数据访问性能。 7. **迁移和版本...

    JavaEE源代码 spring-jdo

    JavaEE源代码 spring-jdoJavaEE源代码 ...jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdo

    jdo2-api-2.3-ec hive hdfs 所需jar

    4. **强大的查询能力**:JDOQL(JDO Query Language)提供了类似SQL的查询功能,可以进行复杂的数据筛选和聚合操作。 5. **事务管理**:JDO提供了丰富的事务控制选项,支持不同级别的事务隔离。 五、在Hive和HDFS中...

    jdo2-api jdo2-api

    jdo2-api jdo2-api jdo2-api jdo2-api

    datanucleus-jdo-jca-5.1.0-release.zip

    标题中的"datanucleus-jdo-jca-5.1.0-release.zip"是指DataNucleus JDO(Java Data Objects)与JCA(Java Connector Architecture)的5.1.0版本发布压缩包。DataNucleus是一个开源的对象关系映射(ORM)框架,它允许...

    gaedo-google-jdo-0.2.24.zip

    【标题】"gaedo-google-jdo-0.2.24.zip" 提供的是 Google 的 Java Data Objects (JDO) 库的一个版本,这是一款针对关系数据库的数据持久化框架。Google JDO 允许开发者使用面向对象的方式操作数据库,简化了数据库...

    J2EE Persistence Options - JDO, Hibernate and EJB 3.0.pdf

    ### J2EE持久化选项:JDO、Hibernate与EJB 3.0 #### 概述 在《J2EE Persistence Options - JDO, Hibernate and EJB 3.0》这篇文档中,作者Sridhar Reddy深入探讨了Java 2 Platform, Enterprise Edition (J2EE)环境...

    querydsl-jpa-2.0.8.zip

    5. 执行查询并获取结果。 【jquery-toastmessage-plugin.zip】 jQuery Toastmessage Plugin是一个用于Web开发的JavaScript插件,它允许你在网页上创建类似于Android系统的Toast消息提示。这种提示通常用于短暂显示...

    querydsl-jpa-codegen-3.3.3.zip

    Querydsl 是一个强大的Java查询语言库,它提供了一种类型安全的方式来表达SQL、JDO和JPA查询。在3.3.3版本中,Querydsl 提供了JPA Codegen工具,这是一款代码生成器,能够自动生成基于Querydsl的JPA查询实体类。通过...

    db-jdo-site:Apache JDO项目

    可以更新如下: 通过在api子模块中调用mvn clean install -Papache-release在db-jdo存储库中创建javadoc jar(例如jdo-api-3.2-javadoc.jar)。 在docs下创建一个新文件夹,例如docs / api32。 将javadocs jar info...

    gaedo-google-instrumentation-jdo-0.4.9.jar

    实测可用

    gaedo-google-instrumentation-jdo-0.4.10.jar

    实测可用

    gaedo-google-instrumentation-jdo-0.4.6.jar

    实测可用

Global site tag (gtag.js) - Google Analytics