我想将下面的查询转换为hql以返回List<Article>
,因为使用createSqlQuery是不可能的,我必须手动转换结果:
这是sql查询:
Query query = getSessionFactory().getCurrentSession().createSQLQuery(
"select *
from article
where articleID in (select articleID
from article_depot
where depotID = "+depotID+")"
);
提前谢谢
答案 0 :(得分:2)
假设article_depot
和article
之间存在 OneToMany 关系(One Depot包含多篇文章),如果已正确映射,则查询将为:
Query query = getSessionFactory().getCurrentSession().createQuery("SELECT d.article from article_depot d where d.depotId = :depotId");
query.setParameter("depotId", depotId);
List<article> resultList = query.getResultList();