Guide to Java Data Objects – Java数据对象指南

最后修改: 2017年 5月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

The Java Data Objects is an API designed for persisting object-oriented data into any database and provide a user-friendly query language using the Java syntax.

Java数据对象是一个API,旨在将面向对象的数据持久化到任何数据库中,并提供一个使用Java语法的用户友好查询语言。

In this article, we are going to see how to use the JDO API for persisting our objects in a database.

在这篇文章中,我们将看到如何使用JDO API将我们的对象持久化在数据库中。

2. Maven Dependencies and Setup

2.Maven的依赖性和设置

We are going to use DataNucleus JDO API that is up to date and offers full support for the JDO 3.2 API.

我们将使用DataNucleus JDO API,它是最新的,并提供对JDO 3.2 API的完全支持。

Let’s add the following dependency to our pom.xml file:

让我们在我们的pom.xml文件中添加以下依赖关系。

<dependency>
    <groupId>org.datanucleus</groupId>
    <artifactId>javax.jdo</artifactId>
    <version>3.2.0-m6</version>
</dependency>
<dependency>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-core</artifactId>
    <version>5.1.0-m2</version>
</dependency>
<dependency>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-api-jdo</artifactId>
    <version>5.1.0-m2</version>
</dependency>
<dependency>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-rdbms</artifactId>
    <version>5.1.0-m2</version>
</dependency>
<dependency>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-xml</artifactId>
    <version>5.0.0-release</version>
</dependency>        

The latest versions of the dependencies can be found here: javax.jdo, datanucleus-core, datanucleus-api-jdo, datanucleus-rdbms and datanucleus-xml.

最新版本的依赖项可以在这里找到。javax.jdodatanucleus-coredatanucleus-api-jdodatanucleus-rdbmsdatanucleus-xml

3. Model

3.模型

We are going to save our data in a database, and before we can do that, we need to create a class that will be used by JDO for storing our data.

我们将把我们的数据保存在数据库中,在这之前,我们需要创建一个将被JDO用于存储数据的类。

To do that we need to create a class with some properties and annotate it with the @PersistentCapable:

要做到这一点,我们需要创建一个具有一些属性的类,并使用@PersistentCapable:对其进行注释。

@PersistenceCapable
public class Product {
 
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
    long id;
 
    String name;
    Double price = 0.0;

    // standard constructors, getters, setters
}

We also annotated our primary key and the chosen strategy.

我们还注释了我们的主键和选择的策略。

Once we create our object, we need to run the enhancer to generate the bytecode required by JDO. Using Maven, we can run this command:

一旦我们创建了对象,我们就需要运行增强器来生成JDO所需的字节码。使用Maven,我们可以运行这个命令。

mvn datanucleus:enhance

This step is mandatory. Otherwise, we get compile time error that the class is not enhanced.

这个步骤是强制性的。否则,我们会得到编译时的错误,即该类未被增强。

Of course, it’s possible to do this automatically during a Maven build:

当然,也可以在Maven构建过程中自动完成这项工作。

<plugin>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-maven-plugin</artifactId>
    <version>5.0.2</version>
    <configuration>
        <api>JDO</api>
        <props>${basedir}/datanucleus.properties</props>
        <log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>
        <verbose>true</verbose>
    </configuration>
    <executions>
        <execution>
            <phase>process-classes</phase>
            <goals>
                <goal>enhance</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The latest version of the plugin can be found here: datanucleus-maven-plugin

该插件的最新版本可以在这里找到。datanucleus-maven-plugin</a

4. Persisting Objects

4.持久化对象

We get access to the database using a JDO factory that gives us the transaction manager that is in charge of performing transactions:

我们通过一个JDO工厂获得对数据库的访问,该工厂给了我们负责执行事务的事务管理器。

PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
PersistenceManager pm = pmf.getPersistenceManager();

Transactions are used to allow a rollback in case of an error:

事务是用来在发生错误时允许回滚的。

Transaction tx = pm.currentTransaction();

We make our transactions inside a try/catch block:

我们在一个try/catch块内进行交易。

Product product = new Product("Tablet", 80.0);
pm.makePersistent(product);

In our finally block, we define these operations to be done in the case of a failure.

在我们的finally块中,我们定义了在失败的情况下要做的这些操作。

If for any reason the transaction cannot be completed, we make a rollback, and we also close the connection to the database with pm.close():

如果由于任何原因,交易无法完成,我们会进行回滚,同时用pm.close()关闭与数据库的连接。

finally {
    if (tx.isActive()) {
        tx.rollback();
    }
    pm.close();
}

To connect our program to the database, we need to create a persistence-unit at runtime to specify the persistent classes, the database type, and connection parameters:

为了将我们的程序连接到数据库,我们需要在运行时创建一个persistence-unit来指定持久化类、数据库类型和连接参数。

PersistenceUnitMetaData pumd = new PersistenceUnitMetaData(
  "dynamic-unit", "RESOURCE_LOCAL", null);
pumd.addClassName("com.baeldung.jdo.Product");
pumd.setExcludeUnlistedClasses();
pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
pumd
  .addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
pumd.addProperty("datanucleus.autoCreateSchema", "true");

5. Reading Objects

5.阅读对象

To read data from our database inside our transaction block, we create a query. Then, we store these items into a Java List collection, which will hold an in-memory copy of the information from the persistent storage.

为了从我们的事务块内的数据库中读取数据,我们创建了一个查询。然后,我们将这些项目存储到一个Java List集合中,该集合将保存来自持久性存储的信息的内存拷贝。

The persistence manager gives us an access to the query interface that allows us to interact with the database:

持久性管理器给了我们一个访问查询接口的机会,使我们能够与数据库互动。

Query q = pm.newQuery(
  "SELECT FROM " + Product.class.getName() + " WHERE price < 1");
List<Product> products = (List<Product>) q.execute();
Iterator<Product> iter = products.iterator();
while (iter.hasNext()) {
    Product p = iter.next();
    // show the product information
}

6. Updating Objects

6.更新对象

To update objects in the database we need to find the objects that we want to update using a query, then we update the results of the query and commit the transaction:

为了更新数据库中的对象,我们需要使用查询找到我们想要更新的对象,然后更新查询的结果并提交事务。

Query query = pm.newQuery(Product.class, "name == \"Phone\"");
Collection result = (Collection) query.execute();
Product product = (Product) result.iterator().next();
product.setName("Android Phone");

7. Deleting Objects

7.删除对象

Similar to the update procedure, we search for the object first and then delete it using the persistence manager. In those situations, JDO updates the persistent storage:

与更新程序类似,我们首先搜索对象,然后使用持久化管理器删除它。在这些情况下,JDO更新持久性存储。

Query query = pm.newQuery(Product.class, "name == \"Android Phone\"");
Collection result = (Collection) query.execute();
Product product = (Product) result.iterator().next();
pm.deletePersistent(product);

8. XML Datastores

8.XML数据存储

Using the XML plugin, we can use XML files for persisting our data.

使用XML插件,我们可以使用XML文件来持久化我们的数据。

We specify our ConnectionURL indicating that is an XML file and specifying the name of the file:

我们指定我们的ConnectionURL表明是一个XML文件,并指定文件的名称:

pumdXML.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myPersistence.xml");

An XML datastore does not support the auto-increment property, so we need to create another class:

一个XML数据存储不支持自动递增属性,所以我们需要创建另一个类。

@PersistenceCapable()
public class ProductXML {

    @XmlAttribute
    private long productNumber = 0;
    @PrimaryKey
    private String name = null;
    private Double price = 0.0;
 
    // standard getters and setters

The @XmlAttribute annotation denotes that this will appear in the XML file as an attribute of the element.

@XmlAttribute注解表示这将作为该元素的一个属性出现在XML文件中。

Let’s create and persist our product:

让我们创建并坚持我们的产品。

ProductXML productXML = new ProductXML(0,"Tablet", 80.0);
pm.makePersistent(productXML);

We get the product stored in the XML file:

我们得到存储在XML文件中的产品。

<productXML productNumber="0">
    <name>Tablet</name>
    <price>80.0</price>
</productXML>

8.1. Recover Objects from the XML Datastore

8.1.从XML数据存储中恢复对象

We can recover our objects from the XML file using a query:

我们可以用查询的方式从XML文件中恢复我们的对象。

Query q = pm.newQuery("SELECT FROM " + ProductXML.class.getName());
List<ProductXML> products = (List<ProductXML>) q.execute();

And then we use the iterator to interact with each object.

然后我们使用迭代器与每个对象进行交互。

9. JDO Queries

9.JDO查询

JDOQL is an object-based query language designed to perform queries using Java objects.

JDOQL是一种基于对象的查询语言,旨在使用Java对象进行查询。

9.1. Declarative JDOQL

9.1.声明性的JDOQL

Using the declarative query, we declare the parameters and set them using Java, this ensures type safety:

使用声明式查询,我们使用Java声明参数并设置参数,这确保了类型安全。

Query qDJDOQL = pm.newQuery(Product.class);
qDJDOQL.setFilter("name == 'Tablet' && price == price_value");
qDJDOQL.declareParameters("double price_value");
List<Product> resultsqDJDOQL = qDJDOQL.setParameters(80.0).executeList();

9.2. SQL

9.2.SQL

JDO provides a mechanism for executing standard SQL queries:

JDO提供了一个执行标准SQL查询的机制。

Query query = pm.newQuery("javax.jdo.query.SQL", "SELECT * FROM PRODUCT");
query.setClass(Product.class);
List<Product> results = query.executeList();

We use javax.jdo.query.SQL as one parameter for the query object and the second parameter is the SQL itself.

我们使用javax.jdo.query.SQL作为查询对象的一个参数,第二个参数是SQL本身。

9.3. JPQL

9.3. JPQL

JDO provides a mechanism for executing JPA queries as well. We can use the full syntax of the JPA query language:

JDO也提供了一个执行JPA查询的机制。我们可以使用JPA查询语言的完整语法。

Query q = pm.newQuery("JPQL",
  "SELECT p FROM " + Product.class.getName() + " p WHERE p.name = 'Laptop'");
List results = (List) q.execute();

10. Summary

10.总结

In this tutorial, we:

在本教程中,我们。

  • created a simple CRUD application that uses JDO
  • saved and retrieved our data as XML
  • examined common query mechanisms

As always, you can find the code from the article over on Github.

一如既往,你可以从Github上的文章中找到相关代码。