Hibernate自定义排序和java.text.Collat​​or

时间:2013-03-01 07:28:54

标签: java hibernate sorting hibernate-4.x

我需要根据字段按asc或dsc顺序对结果集进行排序。该字段的类型为String,包含用户名。现在名字是法国名字。因此,为了根据用户的名字对用户列表进行排序,我使用以下代码:

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

在这种情况下,我从数据库中加载了users的完整列表,然后我正在进行测试。

现在,以下代码用于hibernate,其中startIndexFirstResult设置CriteriamaxResult:设置MaxResults Criteria 1}}和排序:

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 个答案:

答案 0 :(得分:2)

Hibernate不进行排序。数据库确实如此。执行条件查询可归结为执行具有order by fullName子句的SQL查询。

因此,请检查数据库的配置,以了解如何指定表或列使用的排序规则。

答案 1 :(得分:0)

ALTER DATABASE adb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

完成了这项工作。