hibernate 配置Set排序
2009-10-26 11:56
260 查看
hibernate 中对set对象的两种配置方法:
(一)使用order-by对set排序,只需要修改set设置:
<set name="standards" ... order-by="st_id desc"
>
...
</set>
//注意:st_id必须是数据表里的字段名,不能使类里的属性名
//使用order-by,实质是hibernate自动在查询语句后面添加order by ...语句。
(二)使用sort对set排序
1.修改set设置:
<set name="standards" ... sort="mypack.module.AsStandard"
>
...
</set>
注意:
属性standards要定义成:
private Set<AsStandard> standards = new TreeSet
<AsStandard>();
不能定义成HashSet
,否则不能用sort进行排序。
2.修改实体类AsStandard:
public class AsStandard implements Comparator
{ // 1.实现接口Comparator
...
public int compare
(Object o1, Object o2) { // 2.实现方法compare
if(o1 instanceof AsStandard && o2 instanceof AsStandard){
AsStandard r1 = (AsStandard)o1;
AsStandard r2 = (AsStandard)o2;
if(r1.stId>r2.stId){ //stId是类里的属性名,不是数据表里的字段名
return 1;
}
}
return -1;
}
}
//如果支持泛型,可写成:
public class AsStandard implements Comparator<AsStandard>
{ // 1.实现接口Comparator
...
public int compare(AsStandard o1, AsStandard o2
) { // 2.实现方法compare
if(o1.stId>o2.stId){
return -1;
}
return 1;
}
}
(一)使用order-by对set排序,只需要修改set设置:
<set name="standards" ... order-by="st_id desc"
>
...
</set>
//注意:st_id必须是数据表里的字段名,不能使类里的属性名
//使用order-by,实质是hibernate自动在查询语句后面添加order by ...语句。
(二)使用sort对set排序
1.修改set设置:
<set name="standards" ... sort="mypack.module.AsStandard"
>
...
</set>
注意:
属性standards要定义成:
private Set<AsStandard> standards = new TreeSet
<AsStandard>();
不能定义成HashSet
,否则不能用sort进行排序。
2.修改实体类AsStandard:
public class AsStandard implements Comparator
{ // 1.实现接口Comparator
...
public int compare
(Object o1, Object o2) { // 2.实现方法compare
if(o1 instanceof AsStandard && o2 instanceof AsStandard){
AsStandard r1 = (AsStandard)o1;
AsStandard r2 = (AsStandard)o2;
if(r1.stId>r2.stId){ //stId是类里的属性名,不是数据表里的字段名
return 1;
}
}
return -1;
}
}
//如果支持泛型,可写成:
public class AsStandard implements Comparator<AsStandard>
{ // 1.实现接口Comparator
...
public int compare(AsStandard o1, AsStandard o2
) { // 2.实现方法compare
if(o1.stId>o2.stId){
return -1;
}
return 1;
}
}
相关文章推荐
- hibernate 配置Set排序
- Hibernate配置文件和映射元素解释
- 给hibernate配置文件加密解密的方案
- Hibernate配置文件基础配置
- hibernate配置连接池的3中方式
- Hibernate中的Inverse和cascade属性配置
- Hibernate全局配置文件中常用标记的使用
- 配置tomcat数据库连接池及使用数据库连接池(hibernate数据源配置和使用)
- Hibernate配置文件和映射元素解释
- Hibernate_1_环境的配置
- hibernate加载配置文件的两种方式
- Hibernate配置文件和映射元素解释
- mysql hibernate主键配置
- hibernate hibernate4之JPA规范配置详解
- 配置spring+hibernate
- 配置hibernate
- Hibernate配置文件详解
- Hibernate建立关系配置(hbm.xml)bag中cascade属性
- Hibernate基础之配置文件和session
- ssh整合:将hibernate的配置信息,配置到Spring的配置文件中(Spring整合hibernate)