做“IN”动作使用 Hibernate 进行查询

发布于 2024-09-07 00:33:18 字数 578 浏览 2 评论 0原文

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

我的代码是:

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();

但这失败了: 发生 JPAQueryException :从 models.TrackedItem 执行查询时出错,其中 id IN (?): java.util.ArrayList 无法转换为 java.lang.Long

如何使 IN 部分工作?谢谢。

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

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

How can I make the IN part work? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓酥 2024-09-14 00:33:18

您的 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();

要么(使用命名参数):

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();

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

4.6.4.1 位置参数

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

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

4.6.4.2 命名参数

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

示例:

<前><代码>选择c
来自客户 c
WHERE c.status = :stat

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

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

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:

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

挥剑断情 2024-09-14 00:33:18

如果您不幸使用较旧的非 JPA hibernate,那么这应该适合您:

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();

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

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();
温馨耳语 2024-09-14 00:33:18

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

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

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

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

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().

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文