-->
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.  [ 2 posts ] 
Author Message
 Post subject: Nulls first (or last) in order by clause
PostPosted: Thu Nov 22, 2012 5:44 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hi,

Different database servers order null values differently, that is, the null values come first for some but come last for others, when using an order by clause.

Is there any way to have this specified in a portable way across database servers ?

The idea is to have it work on MySql and Oracle without resorting to database server specific syntax.

Thanks.


Top
 Profile  
 
 Post subject: Re: Nulls first (or last) in order by clause
PostPosted: Wed Nov 28, 2012 5:35 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
The way I did it was to have an additional resource directory in the Maven Oracle profile, so as to allow the base abstract test class to include a bean definition file spring-hibernate-custom-dao.xml containing some Oracle specific DAO:
Code:
   <bean id="navbarLanguageCustomDao"
class="com.thalasoft.learnintouch.core.dao.oracle.hibernate.NavbarLanguageCustomHibernateDao">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>


The custom DAO is as follows:
Code:
public interface NavbarLanguageCustomDao extends GenericDao<NavbarLanguage, Serializable> {

   public List<NavbarLanguage> findWithNavbar(Navbar navbar);

}


Code:
@Repository
@Transactional
public class NavbarLanguageCustomHibernateDao extends
      GenericHibernateDao<NavbarLanguage, Serializable> implements
      NavbarLanguageCustomDao {

   @SuppressWarnings("unchecked")
public List<NavbarLanguage> findWithNavbar(Navbar navbar) {
      String statement = "select id, version, language_code as languageCode from navbar_language where navbar_id = :navbarId order by language_code nulls first";
      Query query = getSession().createSQLQuery(statement)
         .addScalar("id", StandardBasicTypes.INTEGER)
         .addScalar("version", StandardBasicTypes.INTEGER)
         .addScalar("languageCode");
      query.setInteger("navbarId", navbar.getId());
      return query.setResultTransformer(Transformers.aliasToBean(NavbarLanguage.class)).list();
   }
}


And it is included in the base test class with the annotation:
Code:
@ContextConfiguration(locations = { "classpath:spring-hibernate.xml", "classpath:spring-hibernate-custom-dao.xml", "classpath:spring-hibernate-dao.xml", "classpath:spring-data-source.xml", "classpath:log4j.xml" })
public abstract class AbstractDaoTest extends AbstractTransactionalJUnit4SpringContextTests {

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.