-->
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: Oracle select fine on the command line is funky in Hibernate
PostPosted: Mon Dec 10, 2012 4:32 am 
Pro
Pro

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

I have a DAO method that is coded as such:
Code:
   public Page<Document> findPublished(final int pageNumber, final int pageSize) {
      Criteria criteria = getSession().createCriteria(getPersistentClass());
      criteria.add(Restrictions.ne("hide", true));
      criteria.addOrder(Order.asc("documentCategory")).addOrder(Order.asc("listOrder"));
logger.debug("================>>> Size1: " + criteria.list().size());
      Page<Document> page = getPage(pageNumber, pageSize, criteria);
logger.debug("================>>> Size2: " + page.getPageItems().size());
      return page;
   }


The method is called by an integration test fed with the following data:
Code:
   public DocumentDaoTest() {
      documentCategory0 = new DocumentCategory();
      documentCategory0.setName("images");
      documentCategory0.setListOrder(1);
      document0 = new Document();
      document0.setFilename("image0.png");
      document0.setListOrder(1);
      document0.setDocumentCategory(documentCategory0);
      document1 = new Document();
      document1.setFilename("pdf1.pdf");
      document1.setListOrder(2);
      document2 = new Document();
      document2.setFilename("pdf2.pdf");
      document2.setListOrder(3);
      document2.setDocumentCategory(documentCategory0);
   }

   public void beforeAnyTest() throws Exception {
      document1 = documentDao.makePersistent(document1);
      document0 = documentDao.makePersistent(document0);
      document2 = documentDao.makePersistent(document2);
   }

   public void testFindPublished() {
      document0.setHide(true);
      Page<Document> page = documentDao.findPublished(0, 0);
      List<Document> documents = page.getPageItems();
      assertEquals(2, documents.size());
      assertEquals(document1.getListOrder(), documents.get(0).getListOrder());
      assertEquals(document2.getListOrder(), documents.get(1).getListOrder());
   }


The test works fine against MySql, H2 and HSQLDB.

But when the test is run against Oracle, the list size is 3 instead of the expected 2.

Here is the console output:
Quote:
testFindPublished(com.thalasoft.learnintouch.core.dao.DocumentDaoTest) Time elapsed: 12.744 sec <<< FAILURE!
java.lang.AssertionError: expected:<2> but was:<3>


I output the actual sql statements with the log4jdbc library and it shows:
Quote:
insert into document (version, filename, hide, secured, list_order, id) values (0, 'pdf1.pdf', 0, 0, 2, 776)
insert into document_category (version, name, list_order, id) values (0, 'images', 1, 477)
insert into document (version, filename, hide, secured, list_order, category_id, id) values (0, 'image0.png', 0, 0, 1, 477, 777)
insert into document (version, filename, hide, secured, list_order, category_id, id) values (0, 'pdf2.pdf', 0, 0, 3, 477, 778)
update document set version=1, hide=1 where id=777 and version=0
select this_.id as id99_0_, this_.version as version99_0_, this_.reference as reference99_0_, this_.description as descript4_99_0_, this_.filename as filename99_0_, this_.hide as hide99_0_, this_.secured as secured99_0_, this_.list_order as list8_99_0_, this_.category_id as category9_99_0_ from document this_ where this_.hide<>1 order by this_.category_id asc, this_.list_order asc


We can see there are 3 inserts into the document table with 1 update.


Last edited by stephaneeybert on Mon Dec 10, 2012 5:06 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Oracle select fine on the command line is funky in Hibernate
PostPosted: Mon Dec 10, 2012 5:05 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
The problem is that on Oracle the null values of document category are ordered last.

So I did a custom DAO method for Oracle:
Code:
   public Page<Document> findPublished(final int pageNumber, final int pageSize) {
      String query = "from Document where hide != :hide order by coalesce(documentCategory, '0')";
      Map<String, Object> parameters = new HashMap<String, Object>();
      parameters.put("hide", true);
      Page<Document> page = getPage(pageNumber, pageSize, query, parameters, getSession());
      return page;
   }


And the test works fine now.


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.