我用的是SSH2,一般比较喜欢 getHibernateTemplate() 这样查询。
我现在有一张表 topic ,有主键 topicId ,不是主键的userId,不是主键的 time ,我想要的是查询 几个不同userId 下的topic 对象 ,并根据它们的发表时间 time 排序,最后取用前10个 topic。
顿时就不知道怎么办了,我是应该把我需要的几个 userId 都穿进去,取得所有 相应的 topic ,然后再 拿出来根据time 排序么?还有,用很多个 userId 去查询我没弄过。 请大神现身啊。

解决方案 »

  1.   

    楼主你这边的topic和user是多对一关系还是一对一关系哦。。只看到你topic里面有外键
    不管什么关系可以用
    getHibernateTemplate().execute(new HibernateCallback<List<Topic>>() {
    @SuppressWarnings("unchecked")
    @Override
      public List<Topic> doInHibernate(Session session) throws  HibernateException,SQLException {
          //这里是按时间的降序排的
          return session.createQuery("select t from Topic t where t.userId in(:userIds) order by t.time desc ")
               .setParameterList("userIds", userIds)//userIds是userId数组或者是集合    .setFirstResult(0);
                       .setMaxResults(10)//取前十个
       .list();
    }
    });
      

  2.   

    上面有个问题userIds如果是数组需要Arrays.asList<userIds>换成集合类型设置进去
    因为setParameterList方法第二个参数只接受Collection