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.
试试看
改为select r.* from reply r where r.topic_id in(1,6) group by topic_id order by topic_id desc.
试试看
解决方案 »
- jsp在线考试系统,上一页和下一页时默认显示学生的答案的问题?
- 请问如何调用ocx控件 js 我在线等
- hibernate中多对一表间关联,使用HQL语句时报错!
- ssh+dwr
- org.hibernate.NonUniqueObjectException
- 各位帮个忙吧 实在不行啊!
- Servlet 练习
- xmldbms中xml元素与数据库表中字段对应关系问题??
- 如何配置jsp和javabean和servlet服务器(用tomcat5.0或weblogic8)以及在jbuilder9里配置使用
- 请给我解释一下java中[工厂模式]
- jboss启动:org.hibernate.MappingException: entity class not found:
- 使用DispatchAction跳转页面问题?????
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>
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;
}}
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();
}
}
我之前就试过了在后面加上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 )