我需要按asc或dsc順序依據字段排序結果集。該字段的類型是String幷包含用戶的名稱。現在這些名字是法國的名字。因此,要進行排序基於他們的名字的用戶列表,一般我用下面的代碼:Hibernate自定義排序和java.text.Collator
final Collator collator = Collator.getInstance(Locale.FRANCE);
Comparator<ActivityUserDTO> comparator = new Comparator<ActivityUserDTO>() {
@Override
public int compare(ActivityUserDTO dto1, ActivityUserDTO dto2) {
return collator.compare(dto1.getFullName(), dto2.getFullName());
}
};
Collections.sort(users, comparator);
在這種情況下,我有一個從數據庫加載,然後我做了soring的users
整個列表。
現在下面的代碼是休眠在那裏我有startIndex
:這臺FirstResult
爲Criteria
,maxResult
:這臺Criteria
的MaxResults
和排序:
Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(entityClass);
if (StringUtils.isNotEmpty(sortField)) {
criteria.addOrder(sortOrder ? Order.asc(sortField) : Order.desc(sortField));
}
criteria.setFirstResult(startIndex);
criteria.setMaxResults(maxResult);
這裏sortField
是fullName
這是在法國和sortOrder
可以是true
或false
。
有沒有什麼方法可以按照自定義的方式進行排序,以便通過Collator
完成排序/排序?任何指針都會對我非常有幫助。
我已經看到了一些網站,如:
,他們正在使用Comparator
到Set of Assoicated Objects
排序,但我怎麼能做到這一點,我案件?
這是我User
:
@javax.persistence.Entity
@Table(name = "USER")
public class User extends Entity {
@Transient
private static final long serialVersionUID = -112950002831333869L;
private String username;
private String firstName;
private String lastName;
private String fullName;
private String mail;
private String homePostalAddress;
private String mobile;
private String homePhone;
private Date dateOfBirth;
private Date dateOfJoining;
private Date dateOfRelease;
private boolean active;
private String role;
private Set<Activity> activities;
public User() {
super();
}
@NaturalId
@Column(name = "USERNAME", nullable = false)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "FIRST_NAME")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "LAST_NAME")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name = "FULL_NAME")
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
@Column(name = "MAIL")
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
@Column(name = "HOME_POSTAL_ADDRESS")
public String getHomePostalAddress() {
return homePostalAddress;
}
public void setHomePostalAddress(String homePostalAddress) {
this.homePostalAddress = homePostalAddress;
}
@Column(name = "MOBILE")
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
@Column(name = "HOME_PHONE")
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
@Column(name = "DATE_OF_BIRTH")
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
@Column(name = "DATE_OF_JOINING")
public Date getDateOfJoining() {
return dateOfJoining;
}
public void setDateOfJoining(Date dateOfJoining) {
this.dateOfJoining = dateOfJoining;
}
@Column(name = "DATE_OF_RELEASE")
public Date getDateOfRelease() {
return dateOfRelease;
}
public void setDateOfRelease(Date dateOfRelease) {
this.dateOfRelease = dateOfRelease;
}
@Column(name = "ACTIVE", nullable = false)
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
@Column(name = "ROLE")
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@ManyToMany(cascade = { CascadeType.ALL }, mappedBy = "users", targetEntity = Activity.class)
public Set<Activity> getActivities() {
return activities;
}
public void setActivities(Set<Activity> activities) {
this.activities = activities;
}
}
謝謝你的建議。我們需要在數據庫或表級別中指定排序規則。我在下面添加了一個答案,完成了這項工作。再次感謝。 – 2013-03-01 11:12:48