【问题标题】:How to use the IN clause while using EntityManager with Hibernate如何在将 EntityManager 与 Hibernate 一起使用时使用 IN 子句
【发布时间】:2019-09-05 05:49:04
【问题描述】:

如何使用 entityManager 准确编写以下查询。

Select * from TABLE_NAME where column_name in ('A', 'B');

我尝试了setParametrList() 方法。但是,它说此方法不适用于 Query 类型。

Query query = entityManager.createQuery(
                "Select t from  TableEntity t WHERE t.name =:bd AND t.staus in (:statuses)", TableEntity .class)
                .setParameter("bd", bd)
                .setParameterList("statuses", statuses);

Error: The method setParameterList(String, List<String>) is undefined for the type TypedQuery<TableEntity >

使用session.createQuery 时,同样的方法似乎也能正常工作。对此有何建议。

【问题讨论】:

标签: sql hibernate sqlparameter sql-in


【解决方案1】:

只需在列表中使用setParameter("statuses", statuses)。该方法检查参数是否为集合并展开它。

【讨论】:

  • 同样按照 JPA 标准,语法是 t.staus in :statuses,但我相当肯定 Hibernate 允许 t.staus in (:statuses)
猜你喜欢
  • 2021-08-30
  • 2011-03-21
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
相关资源
最近更新 更多