首先创建一个类实现Comparator接口,然后重写compare方法。如:
public int compare(Object o1, Object o2) { if (o1 instanceof Street && o2 instanceof Street) { Integer s1 = ((Street)o1).getId(); Integer s2 = ((Street)o2).getId(); return s1.intValue()-s2.intValue(); } return 0; }
然后在xml的set标签中加入sort属性,值为上述类的全路径。
list
因为list集合是有序的,所以hibernate会在记录的时候在数据库中生成一列来记录插入的顺序。当从数据库映射实体类时,用来记录list插入顺序的列不能设为not null,否则报错(本人使用oracle和mysql实验是如此,如有成功的请告诉我一下,谢谢!)。