public List<Topic> getTopicsInForum(int id) {
String hql = "from Topic as t where t.forum.forumId=?";
List<Topic> topics = this.getHibernateTemplate().find(hql, id);
System.out.println("topics.size= "+topics.size());
}
return topics;
}
注:
Forum:论坛
Topic:主题
Topic 与 Forum 使用多对一单向关联,根据论坛ID找出属于该论坛的所有主题如上述方法,使用Spring+Hibernate+mysql+jboss,在service层添加事务管理器,得到的某一论坛中所有主题信息,总是得到一个主题,就是数据库里属于该论坛的第一个主题,其他属于该论坛的主题一概查不出来。不知什么原因。注:数据库中的数据记录是我手动写入用来测试的,没有问题

解决方案 »

  1.   

    String hql = "from Topic as t where t.forum.forumId=?"; 
    有没有试过把后面的where语句去掉执行的结果是什么?
      

  2.   

    问题已解决,是我自己昏了头,在此getTopicsInForum(int) 里调用了另外一个方法getLastTopic(int),查处最近在该论坛发表的帖子,在这个方法里调用了getHibernateTemplate().setMaxResults(1),虽然是打印size()之后调用的,显然还是起了作用,getHibernateTemplate()得到的同一个HibernateTemplate.源代码如下:public List<Topic> getTopicsInForum(int id) {
    String hql = "from Topic as t where t.forum.forumId=?";
    List<Topic> topics = this.getHibernateTemplate().find(hql, id);
    System.out.println("topics.size= "+topics.size());
    for(Topic topic : topics){
    topic.setLastReponse(getLastReponse(topic.getTopicId()));
            if(!Hibernate.isInitialized(topic.getAuthor())){
        Hibernate.initialize(topic.getAuthor());
    }
    }
    return topics;
    }private Topic getLastTopic(int id){
    Topic topic = null;
    String hql = "from Topic as t where t.forum.forumId=? order by t.date desc";
    //getHibernateTemplate().setMaxResults(1);
    Iterator it = getHibernateTemplate().find(hql, id).iterator();
    if(it.hasNext()){
    topic = (Topic)it.next();
    if(!Hibernate.isInitialized(topic.getAuthor()))
    Hibernate.initialize(topic.getAuthor());
    }
    return topic;
    }
    就因为注释掉的这一行。。晕,没想到
      

  3.   

    粘错了,是setLastReponse(int),晕,不过一样。。private Reponse getLastReponse(int id){
    Reponse reponse = null;
    String hql = "from Reponse as r where r.topic.topicId=? order by r.date desc";
    //getHibernateTemplate().setMaxResults(1);
    Iterator it = getHibernateTemplate().find(hql, id).iterator();
    if(it.hasNext()){
    reponse = (Reponse)it.next();
    if(!Hibernate.isInitialized(reponse.getAuthor()))
    Hibernate.initialize(reponse.getAuthor());
    }
    return reponse;
    }