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

Part2 - JDO/JPA 批量检索

阅读更多
你好,欢迎来到这个星期的"JDO/JPA Snippets That Work"环节:执行批量的Gets

你知道App Engine datastore支持批量的gets吗?当你已经获得entities的keys,而你又想获得这些keys对应的entities的时候,批量的gets是一个超级高效解决方法。

这里是使用low-leval的Datastore的API的例子:
public Map<Key, Entity> getById(List<Key> keys) { 
      DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); 
      return ds.get(keys); 
}

现在让我们看下如何使用jpa和jdo做同样的事情:

JPA:
@Entity 
public class Book { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Key key; 
    private String title; 
    // additional members, getters and setters 
} 

public List<Book> getById(List<Key> keys) { 
    Query q = em.createQuery("select from " + Book.class.getName() + " where 
key = :keys"); 
    q.setParameter("keys", keys); 
    return (List<Book>) q.getResultList(); 
} 


JDO:
@PersistenceCapable(identityType = IdentityType.APPLICATION) 
public class Book { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Key key; 
    private String title; 
    // additional members, getters and setters 
} 

public List<Book> getById(List<Key> keys) { 
    Query q = pm.newQuery("select from " + Book.class.getName() + " where 
key == :keys"); 
    return (List<Book>) q.execute(keys); 
} 


注意在两个例子中,我们是如何构造一个query,利用key去pull回那些entities的。App Engine JDO/JPA实现了一个以key作过滤条件的,代替底层datastore查询的detects查询。这个做法是对任何类型的主键都有效的,所以无论你用Long,还是一个unencoded的String,或是一个encoded的String,或是一个Key类型,都是有效的。然而,即使这个看上去像一个query,但所有的fetch-related都受到这样的限制:如果你执行的批量get查询在一个transaction里面,所有你想获得的entities必须属于相同的entity group,否则你会得到一个exception.请注意这一点。

下次当你需要pull回多个entities而你又已经有了他们的keys的时候,就可以使用这个datastore的最佳的机制去批量get这些entities了。

英语原文:
Hello again and welcome to this week's installment of JDO/JPA Snippets That
Work: Executing Batch Gets

Did you know that the App Engine datastore supports batch gets?  Batch gets
are a super-efficient way to load multiple entities when you already have
the keys of the entities you want loaded.  Here's an example using the
low-level Datastore API:

 
public Map<Key, Entity> getById(List<Key> keys) { 
      DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); 
      return ds.get(keys); 
  } 


Now lets see how we can accomplish the same thing in JPA and JDO:
JPA:
@Entity 
public class Book { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Key key; 
    private String title; 
    // additional members, getters and setters 
} 

public List<Book> getById(List<Key> keys) { 
    Query q = em.createQuery("select from " + Book.class.getName() + " where 
key = :keys"); 
    q.setParameter("keys", keys); 
    return (List<Book>) q.getResultList(); 
} 


JDO:
@PersistenceCapable(identityType = IdentityType.APPLICATION) 
public class Book { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Key key; 
    private String title; 
    // additional members, getters and setters 
} 

public List<Book> getById(List<Key> keys) { 
    Query q = pm.newQuery("select from " + Book.class.getName() + " where 
key == :keys"); 
    return (List<Book>) q.execute(keys); 
} 


Notice how in both examples we're constructing a query to pull back the
entities by key.  The App Engine JDO/JPA implementation detects queries that
are filtering only by key and fulfills them using a low level batch get
rather than a datstore query.  This works no matter the type of your primary
key field, so whether you're using a Long, an unencoded String, an encoded
String, or a Key, the same technique will work.  However, even though this
looks like a query, all the fetch-related transaction restrictions apply: if
you're executing your batch get query inside a txn, all of the entities
you're attempting to fetch must belong to the same entity group or you'll
get an exception. Be careful with this.

The next time you need to pull back multiple entities and you already have
their keys, issue a query that filters only by your object's key field and
reap the benefits of the datastore's optimized batch get mechanism.

Until next time....
Max

转载自:http://groups.google.com/group/google-appengine-java/browse_thread/thread/d907ba54c579e9ed?hl=en#

说明:上面的中文翻译是我用自己能理解的方式翻译出来的,并不是逐字翻译,只作为自己参考用,并不十分精确。
分享到:
评论

相关推荐

    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)。

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

    JDO2-API-2.3-EC是Java对象持久化的一个重要实现,它定义了数据存储和检索的接口,使得对象可以被透明地存储到关系数据库、NoSQL数据库甚至是对象数据库中。此版本引入了若干改进和新特性,以提升性能和可扩展性,...

    jdo2-api jdo2-api

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

    xdoclet-plugin-jdo-1.0.3.zip

    《深入解析xdoclet-plugin-jdo-1.0.3:构建高效JDO应用》 在Java开发领域,XDoclet是一款备受推崇的自动化工具,它通过读取源代码注释来生成各种元数据,如JavaDoc、Hibernate映射文件、EJB组件描述等。而xdoclet-...

    JavaEE源代码 spring-jdo

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

    DataNucleus JPA/JDO访问mysql示例

    **datanucleus-samples-jdo-tutorial-3.2** 这个压缩包内的项目是一个DataNucleus JDO教程的示例,它包含了如何设置和使用DataNucleus JDO来访问MySQL数据库的步骤。你将学习如何配置DataNucleus,定义数据模型,...

    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)框架,它允许...

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

    在《J2EE Persistence Options - JDO, Hibernate and EJB 3.0》这篇文档中,作者Sridhar Reddy深入探讨了Java 2 Platform, Enterprise Edition (J2EE)环境下几种主要的持久化技术:Java Data Objects (JDO)、...

    gaedo-google-jdo-0.2.24.zip

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

    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.6.jar

    实测可用

    gaedo-google-instrumentation-jdo-0.4.10.jar

    实测可用

    gaedo-google-instrumentation-jdo-0.4.9.jar

    实测可用

    gaedo-google-instrumentation-jdo-0.4.5.jar

    实测可用

    gaedo-google-instrumentation-jdo-0.4.8.jar

    实测可用

Global site tag (gtag.js) - Google Analytics