We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

How to sort a HQL query by a colum and just get the top record?

killdurst
killdurst asked
on
Medium Priority
8,214 Views
Last Modified: 2013-11-24
Hi, I have a method that executes a HQL and returns me a "List" object. See the code below. Executing it right now would give me 3 records. See operatorTable.jpg. Take note that the "ID" column is a String. I would like to create a similar method that would return me the largest operator id, which in this case, is "00003". See "findMaxOperatorID()" in my code below.

May I know what's wrong with my query? Thanks.
public List findAll() {
        try {
           String queryString = "from Operator";
           Query queryObject = getSession().createQuery(queryString);
  		 return queryObject.list();
        } catch (RuntimeException re) {
           throw re;
        }
  	}
 
-----
 
    public String findMaxOperatorID() {
        try {
           String queryString = "select top 1 operator.operatorId from Operator as operator order by operator.operatorId desc";
           Query queryObject = getSession().createQuery(queryString);
  		 return queryObject.list().get(0).toString();
        } catch (RuntimeException re) {
           throw re;
        }
  	}

Open in new window

operatorTable.jpg
Comment
Watch Question

select top 1 operator.operatorId from Operator as operator order by operator.operatorId desc

may be above query will nto work in HQL .instead of this,you can write like this..

select max(operator.operatorId) from Operator as operator order by operator.operatorId desc


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
remove the top from query and use:

                 return queryObject.setMaxResults(0).list().get(0).toString();

Author

Commented:
Great! Both of those work. But objects, I had to change the setMaxResults(0) to setMaxResults(1) to get it to work. Thanks guys.

Commented:
In the max-Solution "order by" is not necessary

Just a typo in setMaxResults: should be setMaxResults(1) instead of setMaxResults(0)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
sorry for the typo :)

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.