Question

I need to sort a Result set depending on a field by asc or dsc order. That field is of type String and contains names of users. Now the names are french name. So to sort list of users based on their names generally I use the following code:

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

In this case I have the whole list of users loaded from Database and then I am doing the soring.

Now following code is for hibernate where I have startIndex: which sets the FirstResult for Criteria, maxResult: which sets the MaxResults of Criteria and the ordering:

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

Here the sortField is fullName which is in French and sortOrder can be either true or false.

Is there any way to make the ordering in custom manner so that it do the sorting/ordering which is done by the Collator? Any pointer would be very helpful to me.

I have seen some site like:

where they are using Comparator to sort the Set of Assoicated Objects, but how can I do it in my case?

This is my 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;
    }
}
Was it helpful?

Solution

Hibernate doesn't do the sorting. The database does. Executing a criteria query boils down to executing a SQL query having an order by fullName clause.

So check the configuration of your database to know how to specify the collation used by a table or column.

OTHER TIPS

ALTER DATABASE adb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

did the job.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top