大家都知道one-to-many的排序可以通過配置一個order-by="** desc"即可實現
如:
代碼
<set name="clubMessage" table="club_Message" inverse="true" cascade="delete" order-by="create_time desc">
<key column="club_id" />
<one-to-many class="com.test.item.club.domain.ClubMessageVO" />
</set>
上面的hbm屬性意義是:一個社區對應多條社區消息,同時將這些消息按create_time排序
import java.util.Comparator;
import my.User;
public class GBComparator implements Comparator{
public int compare(Object o1, Object o2){
if(o1 == null){
return o2 == null ? 0 : 1;
}
if(o2 == null){
return -1;
}
if(o1 instanceof User && o2 instanceof User){
return ((User)o1).getAccount().compareTo(((User)o2).getAccount());
}
return 0;
}
}
<set
name="users"
table="USER_GROUP_REL"
cascade="none"
inverse="true"
sort="my.GBComparator">
<key column="GROUPING_ID" />
<many-to-many column="USER_ID" class="User" />
</set>