2013-03-01 53 views
2

我需要按asc或dsc順序依據字段排序結果集。該字段的類型是String幷包含用戶的名稱。現在這些名字是法國的名字。因此,要進行排序基於他們的名字的用戶列表,一般我用下面的代碼:Hibernate自定義排序和java.text.Collat​​or

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:這臺FirstResultCriteriamaxResult:這臺CriteriaMaxResults和排序:

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); 

這裏sortFieldfullName這是在法國和sortOrder可以是truefalse

有沒有什麼方法可以按照自定義的方式進行排序,以便通過Collator完成排序/排序?任何指針都會對我非常有幫助。

我已經看到了一些網站,如:

,他們正在使用ComparatorSet 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; 
    } 
} 

回答

2

Hibernate不進行排序。數據庫確實如此。執行標準查詢歸結爲執行具有order by fullName子句的SQL查詢。

因此,請檢查數據庫的配置,以瞭解如何指定表或列使用的排序規則。

+0

謝謝你的建議。我們需要在數據庫或表級別中指定排序規則。我在下面添加了一個答案,完成了這項工作。再次感謝。 – 2013-03-01 11:12:48

0
ALTER DATABASE adb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; 

完成了這項工作。