-->
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: criteria question involving nulls
PostPosted: Thu Apr 20, 2006 4:07 pm 
Newbie

Joined: Thu Apr 06, 2006 6:06 pm
Posts: 10
I have a table with a few bits of data and a varchar column called deleted that contains a single character 'Y' or 'N', or null, in the case where
for some reason or other nothing has been inserted yet.
I am using the Criteria object to create the query like so:
Code:
          Session session = HibernateUtil.currentSession();
          Criteria crit = session.createCriteria(TypeVOPS.class);
       
          if(vo.getCategory() != null)
          {
            crit.add(Restrictions.eq("category", vo.getCategory()));
          }
          if(vo.getCode() != null)
          {
            crit.add(Restrictions.eq("code", vo.getCode())); 
          }
          if(vo.getDescription() != null)
          {
              crit.add(Restrictions.eq("description", vo.getDescription()));
          }
          if (vo.getDeleted() != null && !vo.getDeleted().booleanValue())
          {
              crit.add(Restrictions.ne("deleted", Boolean.TRUE));
          }
          else
          {
              crit.add(Restrictions.eq("deleted", Boolean.TRUE));
          }
          crit.addOrder(Order.asc("code"));
          types = crit.list();

The generated SQL (show_sql=true):
Code:
SELECT   this_.type_id AS type1_0_, this_.category_id AS category2_40_0_,
         this_.code AS code40_0_, this_.description AS descript4_40_0_,
         this_.deleted AS deleted40_0_,
         this_.short_description AS short6_40_0_
    FROM hill.tbl_type_ps this_
   WHERE this_.category_id = 18 AND this_.deleted='N'
ORDER BY this_.code ASC;


Is it possible to get something like this?:
Code:
SELECT   this_.type_id AS type1_0_, this_.category_id AS category2_40_0_,
         this_.code AS code40_0_, this_.description AS descript4_40_0_,
         this_.deleted AS deleted40_0_,
         this_.short_description AS short6_40_0_
    FROM hill.tbl_type_ps this_
   WHERE this_.category_id = 18 AND NVL(this_.deleted, 'N') = 'N'
ORDER BY this_.code ASC;


Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 5:34 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I can't come up with an exact solution, but this workaround should do:
Code:
if (vo.getDeleted() == null)
{
  crit.add(Restrictions.disjunction()
           .add(Restrictions.isNull("deleted"))
           .add(Restrictions.ne("deleted", Boolean.TRUE)));
}
else if (!vo.getDeleted().booleanValue())
{
  crit.add(Restrictions.ne("deleted", Boolean.TRUE));
}
else
{
  crit.add(Restrictions.eq("deleted", Boolean.TRUE));
}


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.