Get All Data from a Table with Hibernate – 用Hibernate从一个表中获取所有数据

最后修改: 2018年 12月 1日

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

1. Overview

1.概述

In this quick tutorial, we’ll look at how to get all data from a table with Hibernate using JPQL or the Criteria API.

在这个快速教程中,我们将看看如何使用JPQL或Criteria API从Hibernate的表中获取所有数据。

JPQL provides us with a quicker and simpler implementation while using the Criteria API is more dynamic and robust.

JPQL为我们提供了一个更快、更简单的实现方式,而使用Criteria API则更有活力和稳健。

2. JPQL

2.JPGL

JPQL provides a simple and straightforward way to get all entities from a table.

JPQL提供了一种简单明了的方式来从一个表中获取所有实体。

Let’s see what it might look like to retrieve all students from a table using JPQL:

让我们看看使用JPQL从一个表中检索所有的学生是什么样子的。

public List<Student> findAllStudentsWithJpql() {
    return session.createQuery("SELECT a FROM Student a", Student.class).getResultList();      
}

Our Hibernate session’s createQuery() method receives a typed query string as the first argument and the entity’s type as the second. We execute the query with a call to the getResultList() method which returns the results as a typed List.

我们的Hibernate会话的createQuery()方法接收一个类型化的查询字符串作为第一个参数,实体的类型作为第二个参数。我们通过调用getResultList()方法来执行查询,该方法将结果作为类型化的List返回。

Simplicity is the advantage of this approach. JPQL is very close to SQL, and is, therefore, easier to write and understand.

简单性是这种方法的优势。JPQL非常接近SQL,因此,更容易编写和理解。

3. Criteria API

3.标准API

The Criteria API provides a dynamic approach for building JPA queries.

Criteria API为构建JPA查询提供了一种动态方法。

It allows us to build queries by instantiating Java objects that represent query elements. And it’s a cleaner solution if queries are constructed from many optional fields because it eliminates a lot of string concatenations.

它允许我们通过实例化代表查询元素的Java对象来构建查询。如果查询是由许多可选字段构成的,它是一个更简洁的解决方案,因为它消除了大量的字符串连接。

We just saw a select-all query using JPQL. Let’s take a look at its equivalent using the Criteria API:

我们刚刚看到了一个使用JPQL的选择-全部查询。让我们来看看使用Criteria API的等效查询。

public List<Student> findAllStudentsWithCriteriaQuery() {
    CriteriaBuilder cb = session.getCriteriaBuilder();
    CriteriaQuery<Student> cq = cb.createQuery(Student.class);
    Root<Student> rootEntry = cq.from(Student.class);
    CriteriaQuery<Student> all = cq.select(rootEntry);

    TypedQuery<Student> allQuery = session.createQuery(all);
    return allQuery.getResultList();
}

First, we get a CriteriaBuilder which we use to create a typed CriteriaQuery. Later, we set the root entry for the query. And lastly, we execute it with a getResultList() method.

首先,我们得到一个CriteriaBuilder,我们用它来创建一个类型的CriteriaQuery。之后,我们为查询设置根条目。最后,我们用getResultList()方法执行它。

Now, this approach is similar to what we did earlier. But, it gives us complete access to the Java language to articulate greater nuance in formulating the query.

现在,这种方法与我们之前所做的类似。但是,它使我们能够完全访问Java语言,在制定查询时阐述更多的细微差别。

In addition to being similar, JPQL queries and JPA criteria-based queries are equivalently performant.

除了相似之外,JPGL查询和JPA基于标准的查询也具有同等的性能。

4. Conclusion

4.结论

In this article, we demonstrated how to get all entities from a table using JPQL or Criteria API.

在这篇文章中,我们演示了如何使用JPQL或Criteria API从一个表中获取所有实体。

The complete source code for the example is available over on GitHub.

该示例的完整源代码可在GitHub上获得