将sql查询转换为hibernate查询以返回特定对象的列表

时间:2013-04-05 08:15:42

标签: sql hibernate hql converter

我想将下面的查询转换为hql以返回List<Article>,因为使用createSqlQuery是不可能的,我必须手动转换结果:
这是sql查询:

Query query = getSessionFactory().getCurrentSession().createSQLQuery(
                 "select * 
                  from article 
                  where articleID in (select articleID 
                                      from article_depot 
                                      where depotID = "+depotID+")"
              );

提前谢谢

1 个答案:

答案 0 :(得分:2)

假设article_depotarticle之间存在 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();