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层添加事务管理器,得到的某一论坛中所有主题信息,总是得到一个主题,就是数据库里属于该论坛的第一个主题,其他属于该论坛的主题一概查不出来。不知什么原因。注:数据库中的数据记录是我手动写入用来测试的,没有问题
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层添加事务管理器,得到的某一论坛中所有主题信息,总是得到一个主题,就是数据库里属于该论坛的第一个主题,其他属于该论坛的主题一概查不出来。不知什么原因。注:数据库中的数据记录是我手动写入用来测试的,没有问题
有没有试过把后面的where语句去掉执行的结果是什么?
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;
}
就因为注释掉的这一行。。晕,没想到
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;
}