将HQL转换为Criteria

时间:2014-03-20 06:46:24

标签: java hibernate hibernate-criteria

HQL我想在Criteria中进行转换。

HQL

select com from News as news " +
            "join news.comments as com " +
            "where news.id = :id " +
            "order by com.addDate desc"

标准

    DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
    criteria.add(Restrictions.idEq(id));
    DetachedCriteria cComment = criteria.createCriteria("comments");
    cComment.addOrder(Order.desc("addDate"));
    List<Comment> list = (List<Comment>)findByCriteria(cComment, false);

标准正在运作,但返回列表新闻。我想退回Сomments。请帮帮我?

1 个答案:

答案 0 :(得分:1)

尝试这可能会对您有所帮助:

DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
criteria.setProjection( Projections.property("com"), "com"));
criteria.add(Restrictions.idEq(id));
DetachedCriteria cComment = criteria.createCriteria("comments");
cComment.addOrder(Order.desc("addDate"));
List<String> list = (List<String>)findByCriteria(cComment, false);