Hibernate 注解:一、多对多中set集合按指定字段排序

最简单的方法是在配置文件中设置,利用配置文件中的order-by 属性来处理

 
  1. <hibernate-mapping>

  2. <class name="com.adcourse.form.Topics" table="tb_topics">

  3. <id name="id" column="id" type="int">

  4. <generator class="increment"/>

  5. </id>

  6.  
  7. <set name="reply" order-by="datetime asc" inverse="true" cascade="all" lazy="false" >

  8. <key column="topics_id"></key>

  9. <one-to-many class="com.adcourse.form.Reply"/>

  10. </set>

  11. </class>

  12. </hibernate-mapping>

  13.  
  14. <hibernate-mapping>

  15. <class name="com.adcourse.form.Reply" table="tb_reply">

  16. <id name="id" column="id" type="int">

  17. <generator class="increment"/>

  18. </id>

  19. <property name="rdatetime" column="datetime" type="java.util.Date" not-null="true"/>

  20. <many-to-one name="topic" column="topics_id" class="com.adcourse.form.Topics" />

  21. </class>

  22. </hibernate-mapping>


上面在一的一端查询出来的set 里面的记录根据 detetime 的升序来排列,注意:是datetime 不是rdatetime

对于注解形式,可以采用

import javax.persistence.OrderBy;

@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "tblStudyType")
@OrderBy("lessonId ASC")
public Set<TblStudyLesson> getTblStudyLessons() {
return this.tblStudyLessons;
}

的方式来配置set的顺序

猜你喜欢

转载自blog.csdn.net/qq_30725371/article/details/81287785