select r.* from reply r where r.topic_id in(1,6) group by topic_id desc.
改为select r.* from reply r where r.topic_id in(1,6) group by topic_id order by topic_id desc.
试试看

解决方案 »

  1.   

    hibernate对两张表做了映射关联,通过reply访问topic时 ,应该使用r.topic.id的形式 而不是直接访问列名r.topic_id。desc 为排序的关键字,须跟在order by 后。
      

  2.   

    hibernate配置文件:
    reply.hbm.xml
    <hibernate-mapping>
        <class name="com.hibernate.po.Reply" table="reply" catalog="hibernate">
            <id name="id" type="integer">
                <column name="id" />
                <generator class="identity" />
            </id>
            <many-to-one name="topic" class="com.hibernate.po.Topic" >
                <column name="topic_id"  >
    </column>
            </many-to-one>
            <property name="addon" type="timestamp">
                <column name="addon"  />
            </property>
              <property name="content" type="string">
                <column name="content"  />
            </property>
              <property name="user_id" type="integer">
                <column name="user_id"  />
            </property>
        </class>
    </hibernate-mapping>topic配置文件<hibernate-mapping>
        <class name="com.hibernate.po.Topic" table="topic" catalog="hibernate">
            <id name="id" type="integer">
                <column name="id" />
                <generator class="identity" />
            </id>
             <set name="replys">
            <key column="topic_id"/>
            <one-to-many class="com.hibernate.po.Reply"/>
         </set>
              <property name="content" type="string">
                <column name="content"  />
            </property>
        </class>
    </hibernate-mapping>
      

  3.   

    po文件:
    Reply.javapublic class Reply  implements java.io.Serializable {
         private Integer id;
         private Topic topic;
         private String content;
         private Date addon;
         private Integer user_id;
    public String getContent() {
    return content;
    }
    public void setContent(String content) {
    this.content = content;
    }

    public Date getAddon() {
    return addon;
    }
    public void setAddon(Date addon) {
    this.addon = addon;
    }
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public Topic getTopic() {
    return topic;
    }
    public void setTopic(Topic topic) {
    this.topic = topic;
    }
    public Integer getUser_id() {
    return user_id;
    }
    public void setUser_id(Integer user_id) {
    this.user_id = user_id;
    }
    }topic.java文件public class Topic { private Integer id;
    private String  content;
    private Set replys;
    public String getContent() {
    return content;
    }
    public void setContent(String content) {
    this.content = content;
    }
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public Set getReplys() {
    return replys;
    }
    public void setReplys(Set replys) {
    this.replys = replys;
    }}
      

  4.   

    测试类部分代码:
    Test.javapublic static SessionFactory initializeSessionFactory(String xmlName){
    SessionFactory sessionFactory= null;
    try {
    sessionFactory = new Configuration().configure(xmlName)
    .buildSessionFactory();
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return sessionFactory;
    }

    public static void main(String[] arg0){
    Session session=initializeSessionFactory("hibernate.cfg.xml").getCurrentSession();
    session.beginTransaction();
    String sql=" from Reply r where r.topic.id in(1,6) group by r.topic.id order by r.topic.id desc ";
            List list=session.createQuery(sql).list();
    session.getTransaction().commit();
    }
    }
      

  5.   

    to xnfzr 
    我之前就试过了在后面加上order by topic_id desc. 但是只能是在返回的结果集中进行排序.不能满足我要的效果.
    假设表中有5条数据,查询后返回3条,order by只会在这返回的3条结果中进行排序,我需要的是对5条记录进行排序.----------------------------------------------------------------------------------
    to jinsu_st .
    先对你的耐心表示感谢!
    我为了便于理解,直接使用sql进行说明的,没有用HQL,实际上查询的时候写的代码和你所说的一样.!这个问题已经解决了.!使用了子查询解决的.先使用max函数将需要的记录数ID查询出来,这样就满足了order by topic_id desc的效果.
    在使用返回的数据做条件,并使用group by进行分组最后完全满足条件.
    代码如下:
    from reply re where re.id in(
       select max(re2.id) from reply re2 where re2.id in(1,6) group by re2.topic.id )