Java 使用 Hibernate 执行“IN”查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3126366/
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-13 16:40:58  来源:igfitidea点击:

Doing an "IN" query with Hibernate

javahibernateormjpajpql

提问by Amy B

I have a list of IDs in a String, and want to use Hibernate to get the rows with these IDs. TrackedItemis a Hibernate/JPA entity (sorry if I'm getting the naming mixed up here).

我有一个字符串中的 ID 列表,并且想使用 Hibernate 来获取带有这些 ID 的行。TrackedItem是一个 Hibernate/JPA 实体(对不起,如果我在这里混淆了命名)。

My code is:

我的代码是:

String idsText = "380, 382, 386";
ArrayList<Long> ids = new ArrayList<Long>();

for (String i : idsText.split(","))
{
    ids.add(Long.getLong(i));
}

List<TrackedItem> items = TrackedItem.find("id IN (?)", ids).fetch();

But that fails: JPAQueryException occured : Error while executing query from models.TrackedItem where id IN (?): java.util.ArrayList cannot be cast to java.lang.Long

但这失败了: JPAQueryException occured : Error while executing query from models.TrackedItem where id IN (?): java.util.ArrayList cannot be cast to java.lang.Long

How can I make the INpart work? Thanks.

我怎样才能使IN零件工作?谢谢。

采纳答案by Pascal Thivent

The syntax of your JPQL query is incorrect. Either use (with a positional parameter):

您的 JPQL 查询的语法不正确。要么使用(带有位置参数):

List<Long> ids = Arrays.asList(380L, 382L, 386L);
Query query = em.createQuery("FROM TrackedItem item WHERE item.id IN (?1)");
query.setParameterList(1, ids)
List<TrackedItem> items = query.getResultList();

Or (with a named parameter):

或(带有命名参数):

List<Long> ids = Arrays.asList(380L, 382L, 386L);
Query query = em.createQuery("FROM TrackedItem item WHERE item.id IN :ids");
query.setParameterList("ids", ids)
List<TrackedItem> items = query.getResultList();

Below, the relevant sections of the JPA 1.0 specification about parameters:

下面是 JPA 1.0 规范中有关参数的相关部分:

4.6.4.1 Positional Parameters

The following rules apply to positional parameters.

  • Input parameters are designated by the question mark (?) prefix followed by an integer. For example: ?1.
  • Input parameters are numbered starting from 1.
    Note that the same parameter can be used more than once in the query string and that the ordering of the use of parameters within the query string need not conform to the order of the positional parameters.

4.6.4.2 Named Parameters

A named parameter is an identifier that is prefixed by the ":" symbol. It follows the rules for identifiers defined in Section 4.4.1. Named parameters are case sensitive.

Example:

SELECT c
FROM Customer c
WHERE c.status = :stat

Section 3.6.1 describes the API for the binding of named query parameters

4.6.4.1 位置参数

以下规则适用于位置参数。

  • 输入参数由问号 (?) 前缀后跟一个整数指定。例如:?1
  • 输入参数从 1 开始编号。
    请注意,同一参数可以在查询字符串中多次使用,并且查询字符串中参数的使用顺序不需要与位置参数的顺序一致。

4.6.4.2 命名参数

命名参数是以“:”符号为前缀的标识符。它遵循第 4.4.1 节中定义的标识符规则。命名参数区分大小写。

例子:

SELECT c
FROM Customer c
WHERE c.status = :stat

第 3.6.1 节描述了用于绑定命名查询参数的 API

回答by depsypher

If you're unlucky enough to be using older non-JPA hibernate, this should work for you:

如果您不幸使用旧的非 JPA 休眠,这应该适合您:

Query query = session.createQuery("FROM TrackedItem item WHERE item.id IN (:items)");
query.setParameterList("items", Arrays.asList(380L, 382L, 386L));

@SuppressWarnings("unchecked")
List<TrackedItem> results = query.list();

回答by Aleksandar Radulovi?

Even when your query executes correctly, you may face an error if your query parameter contains too many values.

即使您的查询正确执行,如果您的查询参数包含太多值,您也可能会遇到错误。

One possible solution to this problem, if you are using Hibernate 5.1 or newer, is to use Session.byMultipleIds().

如果您使用的是 Hibernate 5.1 或更新版本,则此问题的一种可能解决方案是使用 Session.byMultipleIds()。

session
    .byMultipleIds(TrackedItem.class)
    .multiLoad(1L, 2L, 3L);

For more information, see https://thoughts-on-java.org/fetch-multiple-entities-id-hibernate/

有关更多信息,请参阅https://thoughts-on-java.org/fetch-multiple-entities-id-hibernate/