Johnathan Smith
Johnathan Smith

Reputation: 1112

How do I add sorting to this hibernate call in my Spring WebFlow project

How do I add sorting to this hibernate call in my Spring WebFlow project. I am working on a Spring Webflow project and I have to add sorting on the following call.

@SuppressWarnings("unchecked")
    public List<School_lookupModel> getSelectableSchools(String b) {
        String myLike = b + "%";
        Session mySession = sessionFactory.getCurrentSession();
        List<School_lookupModel> mySchools = (List<School_lookupModel>) mySession
                .createCriteria(School_lookupModel.class)
                .add(Restrictions.like("school", myLike)).list();

        return mySchools;
    }

I tryed the following code but it would not build:

public List<School_lookupModel> getSelectableSchools(String b) {

        String myLike = b + "%";

        Session mySession = sessionFactory.getCurrentSession();

        List<School_lookupModel> mySchools = (List<School_lookupModel>) mySession
                .createCriteria(School_lookupModel.class)
            .add(Restrictions.like("school", myLike)
            .addOrder(Order.asc("school"))).list();


        return mySchools;
    }

so can someone please tell me how to add a Order to the first call.. thanks I am getting the following two errors on the build:

Syntax error, insert ")" to complete and The method addOrder(Order) is undefined for the type SimpleExpression

Here is my class:

package org.xxx.schoolvisit.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;

import org.hibernate.annotations.OrderBy;

@Entity
@Table(name = "school")
public class School_lookupModel {

    @Override
    public String toString() {
        return "School_lookupModel [school=" + school + ", school_name="
                + school_name + ", region=" + region + ", district=" + district
                + ", getSchool()=" + getSchool() + ", getSchool_name()="
                + getSchool_name() + ", getRegion()=" + getRegion()
                + ", getDistrict()=" + getDistrict() + ", getClass()="
                + getClass() + ", hashCode()=" + hashCode() + ", toString()="
                + super.toString() + "]";
    }

    @Id
    @Column(name = "school")
    private String school; 

    @Column(name = "school_name")
    private String school_name; 

    @Column(name = "region")
    private String region; 

    @Column(name = "district")
    private String district;

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public String getSchool_name() {
        return school_name;
    }

    public void setSchool_name(String school_name) {
        this.school_name = school_name;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getDistrict() {
        return district;
    }

    public void setDistrict(String district) {
        this.district = district;
    } 


}

Upvotes: 1

Views: 603

Answers (1)

sheidaei
sheidaei

Reputation: 10342

you are missing a paranthesis at this level

  .add(Restrictions.like("school", myLike)
  .addOrder(Order.asc("Name")).list();

make it like this

 .add(Restrictions.like("school", myLike)
  .addOrder(Order.asc("Name"))).list();

what's the error you are getting? Looking at the example here: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html it seems that your syntax is correct, it must be something with the O/R mapping, are you using annotations for mapping the class to table or xml files? do you have a field called Name or you have name or NAME? have you tried these?

Upvotes: 1

Related Questions