Java 生成 JPA 动态类型查询的最佳实践?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4050111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:57:45  来源:igfitidea点击:

Best practice to generate a JPA dynamic, typed query?

javahibernatejpajpa-2.0

提问by Zeemee

i'm trying to convert a 'TableController'-Class we used (without ORM) to generate dynamic SQL (actually the order column and direction is appended to the SQL).

我正在尝试转换我们使用的“TableController”类(不带 ORM)来生成动态 SQL(实际上订单列和方向已附加到 SQL)。

Think of this 'TableController' as a class that have a function to return a list of Entities of a given class (known at runtime), in a given order (String column/property name, boolean asc/desc, both at runtime).

将此“TableController”视为具有以给定顺序(字符串列/属性名称、布尔值 asc/desc,均在运行时)返回给定类(在运行时已知)的实体列表的函数的类。

The challenge is now, with JPA (Hibernate - but the customer requires to use JPA Interfaces only): How can i realize this without String concatenation, and in a type safe manner?

现在的挑战是,使用 JPA(休眠 - 但客户只需要使用 JPA 接口):如何在没有字符串连接的情况下以类型安全的方式实现这一点?

Thanks!

谢谢!

采纳答案by Pascal Thivent

The challenge is now, with JPA (Hibernate - but the customer requires to use JPA Interfaces only): how can I realize this without String concatenation, and in a type safe manner?

现在的挑战是,使用 JPA(休眠 - 但客户只需要使用 JPA 接口):如何在没有字符串连接的情况下以类型安全的方式实现这一点?

If you're using a JPA 2.0 implementation, I think you should look at the Criteria APIto build dynamic queries.

如果您使用的是 JPA 2.0 实现,我认为您应该查看Criteria API来构建动态查询。

If you're using JPA 1.0, there is no standard way apart from String concatenation (and my suggestion would be to use Hibernate's proprietary Criteria Queries).

如果您使用的是 JPA 1.0,那么除了字符串连接之外没有其他标准方法(我的建议是使用 Hibernate 的专有Criteria Queries)。

The following article might also give you some (concrete) ideas: Hibernate Querying 102 : Criteria API.

下面的文章也可能会给你一些(具体的)想法:Hibernate Querying 102 : Criteria API



Imagine a method that has three parameters: Class entityClass, String orderByColumn, boolean ascending. How would i create a query without string concatenation that gives me all objects of the given class in the correct order?

想象一个具有三个参数的方法:Class entityClass、String orderByColumn、boolean 升序。我将如何创建一个没有字符串连接的查询,以正确的顺序为我提供给定类的所有对象?

With the Criteria API from JPA 2.0, you could do something like this:

使用 JPA 2.0 的 Criteria API,您可以执行以下操作:

public <T> List<T> findAllEntitiesOrderedBy(Class<T> entityClass, String orderByColumn, boolean ascending) {
    CriteriaBuilder builder = em.getCriteriaBuilder();

    CriteriaQuery<T> criteria = builder.createQuery(entityClass);
    Root<T> entityRoot = criteria.from(entityClass);
    criteria.select(entityRoot);
    javax.persistence.criteria.Order order = ascending ? builder.asc(entityRoot.get(orderByColumn))
        : builder.desc(entityRoot.get(orderByColumn));
    criteria.orderBy(order);
    return em.createQuery(criteria).getResultList();
}