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#
分享到:
相关推荐
赠送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....
赠送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 )
/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....
对JDO 1.0/2.0的支持。外部依赖spring-jdbc, JDO API, (spring-web)。
xdoclet-plugin-jdo-1.0.3正是为了将XDoclet的强大自动化功能应用于JDO开发,它允许开发者在Java类的注释中定义JDO元数据,如持久化字段、查询、事务属性等。这些注释会被XDoclet解析,并自动生成相应的JDO配置文件...
5. **查询语言**:了解DataNucleus支持的JPA的JPQL和JDO的QOM(Query Object Model),以及如何执行复杂的查询。 6. **性能优化**:学习如何利用DataNucleus的缓存机制和优化选项提高数据访问性能。 7. **迁移和版本...
JavaEE源代码 spring-jdoJavaEE源代码 ...jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdo
4. **强大的查询能力**:JDOQL(JDO Query Language)提供了类似SQL的查询功能,可以进行复杂的数据筛选和聚合操作。 5. **事务管理**:JDO提供了丰富的事务控制选项,支持不同级别的事务隔离。 五、在Hive和HDFS中...
jdo2-api jdo2-api jdo2-api jdo2-api
标题中的"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" 提供的是 Google 的 Java Data Objects (JDO) 库的一个版本,这是一款针对关系数据库的数据持久化框架。Google JDO 允许开发者使用面向对象的方式操作数据库,简化了数据库...
### J2EE持久化选项:JDO、Hibernate与EJB 3.0 #### 概述 在《J2EE Persistence Options - JDO, Hibernate and EJB 3.0》这篇文档中,作者Sridhar Reddy深入探讨了Java 2 Platform, Enterprise Edition (J2EE)环境...
5. 执行查询并获取结果。 【jquery-toastmessage-plugin.zip】 jQuery Toastmessage Plugin是一个用于Web开发的JavaScript插件,它允许你在网页上创建类似于Android系统的Toast消息提示。这种提示通常用于短暂显示...
Querydsl 是一个强大的Java查询语言库,它提供了一种类型安全的方式来表达SQL、JDO和JPA查询。在3.3.3版本中,Querydsl 提供了JPA Codegen工具,这是一款代码生成器,能够自动生成基于Querydsl的JPA查询实体类。通过...
可以更新如下: 通过在api子模块中调用mvn clean install -Papache-release在db-jdo存储库中创建javadoc jar(例如jdo-api-3.2-javadoc.jar)。 在docs下创建一个新文件夹,例如docs / api32。 将javadocs jar info...
实测可用
实测可用
实测可用