0

我有一个普通的 sql 查询,例如:

  String sql = "select * from Comm c where " +
        "c.id in (select max(c.id) from Comm c, ReadComm rc where c.createdby=1 or (c.createdby = 1 and rc.userid=5) and c.id=rc.commid group by c.commThread) " +
        "and c.isdelete = false order by c.senton desc";

是否可以将此类查询转换为条件?我用谷歌搜索了它,但没有得到这样的例子。

谢谢你。

4

1 回答 1

0

是的,看看 DetachedCriteria 和Subqueries

像这样的东西:

// Create subquery
DetachedCriteria sub = DetachedCriteria.forClass(Comm.class, "sub");
sub.createAlias("sub.readComm", "rc");
sub.add(Restrictions.or(Restrictions.eq("sub.createdby", 1), ...
sub.setProjection(Projections.max("sub.id"));

// Create main query
Criteria.forClass(Comm.class)
   .add(Subqueries.propertyEq("id", sub)) // id property == value of subquery
   .add(Restrictions.eq("isdelete", false))
   .addOrder(Order.desc("senton"))
   .list();
于 2013-07-23T14:00:08.563 回答