-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Info about Criteria Query
PostPosted: Mon Dec 05, 2005 4:08 am 
Newbie

Joined: Mon Dec 05, 2005 3:26 am
Posts: 2
Hi Guys,

I am new to hibernate, so please excuse if this is a very elementary doubt. I tried searching on the net about this, without much success.

In our application we are using the following criteria query.

Session m_HibernateSession = HibernateUtil.currentSession();
Criteria m_SelectionCriteria = m_HibernateSession.createCriteria (nativeVO.class);
m_SelectionCriteria.add(Expression.eq("_Mnemonic", p_LocationCode));
m_SelectionCriteria.add(Expression.sql("ROWNUM=1"));
m_SelectionCriteria.addOrder(Order.desc("_LastModifiedDate"));
m_Native = (nativeVO)m_SelectionCriteria.uniqueResult();

Note that the ROWNUM=1 criteria is added before the Order criteria.

- Does that mean the Order criteria is actually useless?(because we are limiting the number of rows to 1, before sorting it).
- Or is it that that the result is sorted first and then, the first row is returned.

Thanks a lot in advance.


Top
 Profile  
 
 Post subject: Re: Info about Criteria Query
PostPosted: Mon Dec 05, 2005 6:01 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
The_Game wrote:
Hi Guys,

I am new to hibernate, so please excuse if this is a very elementary doubt. I tried searching on the net about this, without much success.

In our application we are using the following criteria query.

Session m_HibernateSession = HibernateUtil.currentSession();
Criteria m_SelectionCriteria = m_HibernateSession.createCriteria (nativeVO.class);
m_SelectionCriteria.add(Expression.eq("_Mnemonic", p_LocationCode));
m_SelectionCriteria.add(Expression.sql("ROWNUM=1"));
m_SelectionCriteria.addOrder(Order.desc("_LastModifiedDate"));
m_Native = (nativeVO)m_SelectionCriteria.uniqueResult();

Note that the ROWNUM=1 criteria is added before the Order criteria.

- Does that mean the Order criteria is actually useless?(because we are limiting the number of rows to 1, before sorting it).
- Or is it that that the result is sorted first and then, the first row is returned.

Thanks a lot in advance.


in this case the orderCriteria is useless because uniqueResult() is supposed to deliver 1 single result set. And it expects only a single result set.

_________________
Please don't forget to give credit, if my posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 6:42 am 
Newbie

Joined: Mon Dec 05, 2005 3:26 am
Posts: 2
I think I might have realized what the mistake is. The above criteria query generates a sql query similar to this

SELECT * FROM lang WHERE ROWNUM=1 ORDER BY SORT_ORDER ASC;

This query always returns the same row, no matter how it is sorted (because ROWNUM=1 always returns the first row). sql then tries to sort the single row :)

Please, correct me if I am missing something. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 8:01 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
The_Game wrote:
I think I might have realized what the mistake is. The above criteria query generates a sql query similar to this

SELECT * FROM lang WHERE ROWNUM=1 ORDER BY SORT_ORDER ASC;

This query always returns the same row, no matter how it is sorted (because ROWNUM=1 always returns the first row). sql then tries to sort the single row :)

Please, correct me if I am missing something. Thanks.


I take it, you're using Oracle. Then your assumption is correct.
Oracle narrows the resultset by applying the where condition. These results are sorted afterwards.

You might be able to do what you want another way:
Try setMaxResults(1) instead of ROWNUM=1.

_________________
hth,
Heinz
Don't forget to rate if this helped


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.