【发布时间】:2011-03-08 18:27:54
【问题描述】:
我有一个字符串中的 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 occured : Error while executing query from models.TrackedItem where id IN (?): java.util.ArrayList cannot be cast to java.lang.Long
如何使IN 部分工作?谢谢。
【问题讨论】:
-
Long.getLong(i)不会像您认为的那样做。请改用Long.parseLong(i)。见stackoverflow.com/questions/7376857/…
标签: java hibernate orm jpa jpql