---
title: "Hibernate中Set映射排序"
author: "bolan392"
date: "2010-08-03T12:40:11Z"
tags: []
categories: ["企业架构"]
slug: "hibernate-set"
draft: false
lastmod: "2010-08-03T12:40:11Z"
id: 727882
url: "https://bolan392.iteye.com/blog/727882"
---

在开发过程中，Hibernate的集合映射大多采用&lt;Set/&gt;标签，由于Set重元素的无序特点，使得在前台页面展现时每次刷新都会看到不同的结果，解决这种问题的一种方法是在进行set映射时指定其中元素的排序规则：

```
 <set name="elExamQueses" inverse="true" cascade="all" order-by="EXAM_QUESE_ID desc">
            <key>
                <column name="EXAM_ID" length="30" />
            </key>
            <one-to-many class="com.order.cc.elearn.exam.examination.entity.ElExamQuesEntity"/>
</set>
```

 此处注意：EXAM\_QUESE\_ID是数据库中的字段名，而不是类中的某个字段，后加desc代表降序，如果不加则代表默认的升序排列。