【发布时间】:2022-06-13 20:08:07
【问题描述】:
假设我有一个名为 Seasons 的表格:
| ... | start_month | end_month |
|---|---|---|
| ... | 2 | 6 |
| ... | 3 | 4 |
| ... | ... | ... |
我需要编写一个查询,其中对于给定的月份列表返回满足列表中至少 1 个月的条件的所有 季节:start_month <= month <= end_month。
我已将此查询编写为使用 JDBC 的本机查询,除了 where 子句。
@Repository
public class SeasonsRepositoryImpl implements SeasonsRepositoryCustom {
@PersistenceContext
private EntityManager em;
@Override
public List<SeasonsProjection> findByMonths(List<Integer> months) {
String query = "select * " +
"from seasons as s" +
"where ...."
try {
return em.createNativeQuery(query)
.setParameter("months", months)
.unwrap(org.hibernate.query.NativeQuery.class)
.setResultTransformer(Transformers.aliasToBean(SeasonsProjection.class))
.getResultList();
} catch (Exception e) {
log.error("Exception with an exception message: {}", e.getMessage());
throw e;
}
}
}
我不知道如何写这个,我想使用 ANY 运算符,直到我发现 ANY 只适用于表而不是列表,我想把它写成一个将列表转换为表的子查询,但是我不知道这是否可能,我在 MySQL 文档中找不到任何内容。任何帮助将不胜感激。
【问题讨论】: